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

where子句提供了一种在操作使用完全匹配时检索数据的方法。 在需要具有共享特征的多个结果的情况下,like子句适应宽模式匹配。

like子句测试模式匹配,返回true或false。 用于比较的模式接受以下通配符:"%",其匹配字符数(0或更多); 和"_",它匹配单个字符。 "_"通配符只匹配其集合中的字符,这意味着当使用另一个集合时,它将忽略拉丁字符。 匹配在默认情况下不区分大小写,需要对大小写敏感的附加设置。

not like子句允许测试相反的条件,非常类似于非运算符。

如果语句表达式或模式求值为null,则结果为null。

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

select field, field2,... from table_name, table_name2,...
where field like condition

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

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

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脚本使用like子句

在使用like子句的语句中使用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 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);
?>

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

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

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