MariaDB学习 专题
您的位置:database > MariaDB学习专题 > MariaDB Where子句
MariaDB Where子句
作者:--    发布时间:2019-11-20

where子句过滤各种语句,如select,update,delete和insert。 他们提出了用于指定行动的标准。 它们通常出现在语句中的表名后面,其条件如下。 where子句本质上像一个if语句。

查看下面给出的where子句的一般语法 -

[command] field,field2,... from table_name,table_name2,... where [condition]

请注意where子句的以下特性:

  • 它是可选的。

  • 它允许指定任何条件。

  • 它允许通过使用and或or运算符来指定多个条件。

  • 区分大小写仅适用于使用like比较的语句。

where子句允许使用以下运算符 -

操作者
= !=
> <
>= <=

where子句可以在命令提示符或php脚本中使用。

在命令提示符下,只需使用标准命令 -

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             |
+-------------+----------------+----------------------+

使用and条件查看示例 -

select *
from products_tbl
where product_name = 'bun janshu 3000';
and product_id <= 344;

此示例组合了and和or条件

select *
from products_tbl
where (product_name = 'bun janshu 3000' and product_id < 344)
or (product_name = 'bun janshu 3000');

php脚本使用where子句

在使用where子句的操作中应用mysql_query()函数 -

<?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);
?>

成功的数据检索后,您将看到以下输出 -

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

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册