w3resource

MySQL not like operator

not like operator

MySQL NOT LIKE operator along with WILDCARDS checks whether a string of a specified pattern is not present within another string.

This function is useful in -

  • Negative pattern matching: Finds rows that do not match a specific pattern.
  • Negation of LIKE: The NOT LIKE operator negates the LIKE operator
  • Complex filtering: NOT LIKE can be combined with other comparison operators, such as AND or OR, to create complex filtering conditions.

Syntax:

NOT LIKE pat

Argument:

Name Description
pat A pattern not to be matched.

MySQL Version: 8.0

Example of MySQL not like operator with wildcard ( _ ) underscore

Sample table: author


Code:

SELECT * FROM author 
WHERE aut_name NOT LIKE '_______________'; 

Relational Algebra Expression:

Relational Algebra Expression: MySQL not like operator with wildcard ( _ ) underscore.

Relational Algebra Tree:

Relational Algebra Tree: MySQL not like operator with wildcard ( _ ) underscore.

Explanation:

The above MySQL statement will return those rows from the table author in which the length of the author’s name is not exactly 15 characters. Fifteen instances of ‘_’ pattern character has been used to indicate 15 characters.

Output:

mysql> SELECT * FROM author 
    -> WHERE aut_name NOT LIKE '_______________';
+--------+----------------------+-----------+----------------+
| aut_id | aut_name             | country   | home_city      |
+--------+----------------------+-----------+----------------+
| AUT001 | William Norton       | UK        | Cambridge      | 
| AUT005 | Thomas Morgan        | Germany   | Arnsberg       | 
| AUT006 | Thomas Merton        | USA       | New York       | 
| AUT007 | Piers Gibson         | UK        | London         | 
| AUT008 | Nikolai Dewey        | USA       | Atlanta        | 
| AUT009 | Marquis de Ellis     | Brazil    | Rio De Janerio | 
| AUT010 | Joseph Milton        | USA       | Houston        | 
| AUT011 | John Betjeman Hunter | Australia | Sydney         | 
| AUT012 | Evan Hayek           | Canada    | Vancouver      | 
| AUT013 | E. Howard            | Australia | Adelaide       | 
| AUT014 | C. J. Wilde          | UK        | London         | 
| AUT015 | Butler Andre         | USA       | Florida        | 
+--------+----------------------+-----------+----------------+
12 rows in set (0.00 sec)

Example of MySQL not like operator with wildcard (%)

Sample table: author


Code:

SELECT * FROM author 
WHERE aut_name NOT LIKE '%t%';

Relational Algebra Expression:

Relational Algebra Expression: MySQL not like operator with wildcard (%).

Relational Algebra Tree:

Relational Algebra Tree: MySQL not like operator with wildcard (%).

Explanation:

The above MySQL statement will return those rows from the table author in which the author’s name does not containing a character ‘t’.

Output:

mysql> SELECT * FROM author 
    -> WHERE aut_name NOT LIKE '%t%';
+--------+------------------+-----------+----------------+
| aut_id | aut_name         | country   | home_city      |
+--------+------------------+-----------+----------------+
| AUT002 | William Maugham  | Canada    | Toronto        | 
| AUT007 | Piers Gibson     | UK        | London         | 
| AUT008 | Nikolai Dewey    | USA       | Atlanta        | 
| AUT009 | Marquis de Ellis | Brazil    | Rio De Janerio | 
| AUT012 | Evan Hayek       | Canada    | Vancouver      | 
| AUT013 | E. Howard        | Australia | Adelaide       | 
| AUT014 | C. J. Wilde      | UK        | London         | 
+--------+------------------+-----------+----------------+
7 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: MID
Next: NOT REGEXP



Follow us on Facebook and Twitter for latest update.