MariaDB Where clause

WHERE clause filters various statements, such as SELECT, UPDATE, DELETE, and INSERT. They proposed a standard for specifying the action.
They usually appear after the table name in the statement, under the following conditions. The WHERE clause is essentially like an if statement.

The general syntax of the WHERE clause of the view given below -

[COMMAND] field,field2,... FROM table_name,table_name2,... WHERE [CONDITION]

Please note that the WHERE clause of the following characteristics:

  • It is optional.

  • It allows you to specify any conditions.

  • It allows the use of AND or OR operator to specify multiple criteria.

  • Only for case-sensitive comparisons using the LIKE statement.

WHERE clause allows operators to use the following

Operators
= !=
> <
>= <=

WHERE clause can be used in a command prompt or PHP scripts.

Command Prompt

At the command prompt, simply use the standard commands

root@host# mysql -u root -p password;
Enter password:*******
mysql> use PRODUCTS;
Database changed
mysql> SELECT * from products_tbl WHERE product_manufacturer = 'XYZ Corp';
+-------------+----------------+----------------------+
| ID_number   | Nomenclature   | product_manufacturer |
+-------------+----------------+----------------------+
| 12345       | Orbitron 4000  | XYZ Corp             |
+-------------+----------------+----------------------+
| 12346       | Orbitron 3000  | XYZ Corp             |
+-------------+----------------+----------------------+
| 12347       | Orbitron 1000  | XYZ Corp             |
+-------------+----------------+----------------------+

Use AND conditions View Sample

SELECT *
FROM products_tbl
WHERE product_name = 'Bun Janshu 3000';
AND product_id <= 344;

This example of a combination of AND and OR conditions

SELECT *
FROM products_tbl
WHERE (product_name = 'Bun Janshu 3000' AND product_id < 344)
OR (product_name = 'Bun Janshu 3000');

PHP script using the Where clause

Application of the operation of the WHERE clause mysql_query () function

<?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 = "XYZ Corp"';

   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 successful data retrieval, you will see the following output

产品编号:12345
命名:Orbitron 4000
制造商:XYZ公司
交货日期:17年1月1日
----------------------------------------------
产品编号:12346
命名:Orbitron 3000
制造商:XYZ公司
交货日期:17年1月2日
----------------------------------------------
产品编号:12347
命名:Orbitron 1000
制造商:XYZ公司
交货日期:17年1月2日
----------------------------------------------
成功的mysql>读取的数据

This switched: http: //codingdict.com/article/7095

Guess you like

Origin www.cnblogs.com/bczd/p/12009695.html