Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Maria DB like clause


May 16, 2021 MariaDB


Table of contents


The WHERE clause provides a way to retrieve data when an operation uses an exact match. T he LIKE clause adapts to wide pattern matching when multiple results with shared characteristics are required.

LIKE clause test pattern match, return true or false. T he pattern used for comparison accepts the following wildcard: %, which matches the number of characters (0 or more); a nd ""," which match a single character. T he wildcard matches only the characters in its collection, which means that when another collection is used, it ignores Latin characters. M atches are case insensitive by default and require additional case-sensitive settings.

The NOT LIKE clause allows you to test the opposite condition, much like a non-operator.

If the statement expression or pattern is evaluated as NULL, the result is NULL.

Check out the generalLIKE clause syntax given below -

SELECT field, field2,... FROM table_name, table_name2,...
WHERE field LIKE condition

Use theLIKE clause in command prompts or PHP scripts.

Command prompt

At the command prompt, simply use the standard command -

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 scripts use the like clause

Use the 10,000-mysql_query in statements that use the LIKE clause

<?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
";
   mysql_close($conn);
?>

After a successful data retrieval, you will see the following output -

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