Tuesday, 10 July 2018

Mysql LIKE operator

Mysql LIKE operator

MySQL LIKE operator along with WILDCARDS finds a string of a specified pattern within another string.
In a more technical note, LIKE operator does pattern matching using simple regular expression comparison.
This is a table which describes the wildcards used with MySQL LIKE operator -
WildcardsDescription
%Matches any number of characters including zero.
_Matches exactly one character.
Syntax:
LIKE pat
Argument
NameDescription
patA pattern which is to be matched.
MySQL Version: 5.6
Example of MySQL LIKE operator with wildcard (%) matching from the beginning
The following MySQL statement will return those rows from the table author in which the name of the author starts with the character ‘W’.
Code:
SELECT * 
FROM author 
WHERE aut_name LIKE 'W%';


Sample table: author

Sample Output:
mysql> SELECT * 
    -> FROM author 
    -> WHERE aut_name LIKE 'W%';
+--------+-----------------+---------+-----------+
| aut_id | aut_name        | country | home_city |
+--------+-----------------+---------+-----------+
| AUT001 | William Norton  | UK      | Cambridge | 
| AUT002 | William Maugham | Canada  | Toronto   | 
| AUT003 | William Anthony | UK      | Leeds     | 
+--------+-----------------+---------+-----------+
3 rows in set (0.04 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 php mysql like function | w3resource</title>
<meta name="description" content="example php mysql like function">
<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 with all their detail available, where name of the author starts with 'W':</h2>
<table class='table table-bordered'>
<tr>
<th>Author's ID</th><th>Author's name</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 * FROM author WHERE aut_name LIKE "W%"') as $row) {
echo "<tr>";  
echo "<td>" . $row['aut_id'] . "</td>";  
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>





Example of MySQL LIKE operator with wildcard (%) matching from the end
The following MySQL statement will return those rows from the table author in which the name of the author ends with the substring ‘on’.
Code:

SELECT * FROM author 
WHERE aut_name LIKE '%on';


Sample table: author

Sample Output:
mysql> SELECT * 
    -> FROM author 
    -> WHERE aut_name LIKE '%on';
+--------+----------------+---------+-----------+
| aut_id | aut_name       | country | home_city |
+--------+----------------+---------+-----------+
| AUT001 | William Norton | UK      | Cambridge | 
| AUT006 | Thomas Merton  | USA     | New York  | 
| AUT007 | Piers Gibson   | UK      | London    | 
| AUT010 | Joseph Milton  | USA     | Houston   | 
+--------+----------------+---------+-----------+
4 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 of like function | w3resource</title>
<meta name="description" content="Example of like function">
<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 with all their detail available, where name of the author ends with 'on':</h2>
<table class='table table-bordered'>
<tr>
<th>Author's ID</th><th>Author's name</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 * FROM author WHERE aut_name LIKE "%on"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_id'] . "</td>";
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>


Example of MySQL LIKE operator with wildcard (%) matching within the string
The following MySQL statement will return those rows from the table author in which the name of the author contains ‘k’.
Code:
SELECT * FROM author 
WHERE aut_name LIKE '%k%';


Sample table: author

Sample Output:
mysql> SELECT * 
    -> FROM author 
    -> WHERE aut_name LIKE '%k%';
+--------+---------------+---------+-----------+
| aut_id | aut_name      | country | home_city |
+--------+---------------+---------+-----------+
| AUT008 | Nikolai Dewey | USA     | Atlanta   | 
| AUT012 | Evan Hayek    | Canada  | Vancouver | 
+--------+---------------+---------+-----------+
2 rows in set (0.00 sec)
Example of MySQL LIKE operator with wildcard (_) underscore
The following MySQL statement will return those rows from the table author in which the length of the author’s name is exactly 12 characters. Twelve ‘_’ have been used to indicate 12 characters.
Code:
SELECT * FROM author 
WHERE aut_name LIKE '____________'; 


Sample table: author

Sample Output:
mysql> SELECT *
    -> FROM author 
    -> WHERE aut_name LIKE '____________'; 
+--------+--------------+---------+-----------+
| aut_id | aut_name     | country | home_city |
+--------+--------------+---------+-----------+
| AUT007 | Piers Gibson | UK      | London    | 
| AUT015 | Butler Andre | USA     | Florida   | 
+--------+--------------+---------+-----------+
2 rows in set (0.00 sec)

0 comments:

Post a Comment