Mariadb 简明教程

MariaDB - Like Clause

WHERE 子句提供了一种在操作使用完全匹配时检索数据的方式。在需要具有共享特征的多条结果的情况下, LIKE 子句可容纳广义模式匹配。

The WHERE clause provides a way to retrieve data when an operation uses an exact match. In situations requiring multiple results with shared characteristics, the LIKE clause accommodates broad pattern matching.

LIKE 子句测试模式匹配,返回真或假。用于比较的模式接受以下通配符:“%”,它与数字字符(0 或更多)匹配;而 “ ”, which matches a single character. The “ ” 通配符仅与它自己集合内的字符匹配,这意味着在使用另一个集合时它将忽略拉丁字符。默认情况不区分大小写,需要附加设置来区分大小写。

A LIKE clause tests for a pattern match, returning a true or false. The patterns used for comparison accept the following wildcard characters: “%”, which matches numbers of characters (0 or more); and “”, which matches a single character. The “” wildcard character only matches characters within its set, meaning it will ignore latin characters when using another set. The matches are case-insensitive by default requiring additional settings for case sensitivity.

NOT LIKE 子句允许测试相反条件,很像 not 运算符。

A NOT LIKE clause allows for testing the opposite condition, much like the not operator.

如果语句表达式或模式评估为 NULL,则结果为 NULL。

If the statement expression or pattern evaluate to NULL, the result is NULL.

复习以下给出的 LIKE 子句一般语法 −

Review the general LIKE clause syntax given below −

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

在命令提示符处或 PHP 脚本中使用 LIKE 子句。

Employ a LIKE clause either at the command prompt or within a PHP script.

The Command Prompt

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

At the command prompt, simply use a 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 Script Using Like Clause

在使用 LIKE 子句的语句中使用 mysql_query() 函数

Use the mysql_query() function in statements employing 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\n";
   mysql_close($conn);
?>

当数据检索成功时,您将看到以下输出 −

On 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