The WHERE statement gives an approach to recover information when an activity utilizes a definite match. In circumstances requiring different outcomes with shared attributes, the LIKE statement obliges wide example coordinating.
A LIKE proviso tests for an example coordinate, restoring a valid or bogus. The examples utilized for correlation acknowledge the accompanying trump card characters: "%", which matches quantities of characters (at least 0); and "_", which coordinates a solitary character. The "_" trump card character just matches characters inside its set, which means it will overlook latin characters when utilizing another set. The matches are case-heartless as a matter of course requiring extra settings for case affectability.
A NOT LIKE proviso considers testing the contrary condition, similar as the not administrator.
In the event that the assertion articulation or example assess to NULL, the outcome is NULL.
Audit the overall LIKE proviso linguistic structure given beneath −
SELECT field, field2,... FROM table_name, table_name2,...
WHERE field LIKE condition
Utilize a LIKE provision either at the order brief or inside a PHP content.
The Command Prompt
At the order brief, just utilize a standard order −
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from products_tbl
WHERE product_manufacturer LIKE 'XYZ%';
+-------------+----------------+----------------------+
| ID_number | Nomenclature | product_manufacturer |
+-------------+----------------+----------------------+
| 12345 | Orbitron 4000 | XYZ Corp |
+-------------+----------------+----------------------+
| 12346 | Orbitron 3000 | XYZ Corp |
+-------------+----------------+----------------------+
| 12347 | Orbitron 1000 | XYZ Corp |
+-------------+----------------+----------------------+
PHP Script Using Like Clause
Utilize the mysql_query() work in explanations utilizing the LIKE statement
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT product_id, product_name, product_manufacturer, ship_date
FROM products_tbl WHERE product_manufacturer LIKE "xyz%"';
mysql_select_db('PRODUCTS');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Product ID:{$row['product_id']} <br> ".
"Name: {$row['product_name']} <br> ".
"Manufacturer: {$row['product_manufacturer']} <br> ".
"Ship Date: {$row['ship_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
On fruitful information recovery, you will see the accompanying yield −
Product ID: 12345
Nomenclature: Orbitron 4000
Manufacturer: XYZ Corp
Ship Date: 01/01/17
----------------------------------------------
Product ID: 12346
Nomenclature: Orbitron 3000
Manufacturer: XYZ Corp
Ship Date: 01/02/17
----------------------------------------------
Product ID: 12347
Nomenclature: Orbitron 1000
Manufacturer: XYZ Corp
Ship Date: 01/02/17
----------------------------------------------
mysql> Fetched data successfully