w3resource
MYSQL Tutorial

MySQL NOT LIKE operator

NOT LIKE operator

MySQL NOT LIKE is used to exclude those rows which are matching the criterion followed by LIKE operator.

Syntax:

expr NOT LIKE pat [ESCAPE 'escape_char']
  • Pattern matching using SQL simple regular expression comparison. Returns 1 (TRUE) or 0 (FALSE). If either expr or pat is NULL, the result is NULL.
  • The pattern need not be a literal string. For example, it can be specified as a string expression or table column.
  • Per the SQL standard, LIKE performs matching on a per-character basis, thus it can produce results different from the = comparison operator.
  • LIKE operator uses WILDCARDS (i.e. %, _) to match the pattern. This is very useful to check whether a particular character or string is present in the records.

% is used to match any number of characters, even zero characters.
_ is used to match exactly one character.

To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the ESCAPE character, “\” is assumed.
\% is used to match one "%" character.
\_ Matches one "_" character

MySQL Version: 5.6

Example: MySQL NOT LIKE operator with (%) percent

The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.

Code:

SELECT aut_name, country
FROM author 
WHERE aut_name NOT LIKE 'W%';

Sample table: author


Sample Output:

mysql> SELECT aut_name, country
    -> FROM author 
    -> WHERE aut_name NOT LIKE 'W%';
+----------------------+-----------+
| aut_name             | country   |
+----------------------+-----------+
| S.B.Swaminathan      | India     | 
| Thomas Morgan        | Germany   | 
| Thomas Merton        | USA       | 
| Piers Gibson         | UK        | 
| Nikolai Dewey        | USA       | 
| Marquis de Ellis     | Brazil    | 
| Joseph Milton        | USA       | 
| John Betjeman Hunter | Australia | 
| Evan Hayek           | Canada    | 
| E. Howard            | Australia | 
| C. J. Wilde          | UK        | 
| Butler Andre         | USA       | 
+----------------------+-----------+
12 rows in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-not-like - php mysql examples | w3resource</title>
<meta name="description" content="example-not-like - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>List of authors whose names are not started with 'W', along with their country:</h2>
<table class='table table-bordered'>
<tr>
<th>Authors</th><th>Country</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE "W%"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-not-like</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT aut_name,country FROM author WHERE aut_name NOT LIKE'W%'";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Authors</td>
<td>Country</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("aut_name")%></TD>
<TD><%=rs.getString("country")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : MySQL NOT LIKE operator with ( _ ) underscore

The following MySQL statement excludes those rows from the table author having the country name like the above pattern as specified with LIKE operator.

Code:

SELECT aut_name, country,home_city            
FROM author          
WHERE country NOT LIKE 'U_A' and country NOT LIKE  'C__a_a';

Sample table: author


Sample Output:

mysql> SELECT aut_name, country,home_city            
    -> FROM author          
    -> WHERE country NOT LIKE 'U_A' and country NOT LIKE  'C__a_a';
+----------------------+-----------+----------------+
| aut_name             | country   | home_city      |
+----------------------+-----------+----------------+
| William Norton       | UK        | Cambridge      | 
| William Anthony      | UK        | Leeds          | 
| S.B.Swaminathan      | India     | Bangalore      | 
| Thomas Morgan        | Germany   | Arnsberg       | 
| Piers Gibson         | UK        | London         | 
| Marquis de Ellis     | Brazil    | Rio De Janerio | 
| John Betjeman Hunter | Australia | Sydney         | 
| E. Howard            | Australia | Adelaide       | 
| C. J. Wilde          | UK        | London         | 
+----------------------+-----------+----------------+
9 rows in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-not-like - php mysql examples | w3resource</title>
<meta name="description" content="example1-not-like - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>List of authors whose countries don't contain 'U_A' or 'C__a_a', along with their country and home city:</h2>
<table class='table table-bordered'>
<tr>
<th>Authors</th><th>Country</th><th>Home city</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT aut_name, country,home_city
FROM author
WHERE country NOT LIKE "U_A" and country NOT LIKE  "C__a_a"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['home_city'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Slideshow of MySQL Comparison Function and Operators

MySQL Comparison Function and Operators, slide presentation

Previous: NOT IN()
Next: MySQL Logical Operators AND operator



New Content: Composer: Dependency manager for PHP, R Programming


bedava porno