mysql like ilike_MySQLilike 子句
我們已經看到SQLSELECT命令從MySQLi表中獲取數據。我們也可以使用一個名為WHERE子句的條件語句來選擇所需的記錄。
等號(=)的WHERE子句可以正常工作,我們要做完全匹配。喜歡“name ="sai"”。但是可能有一個要求,我們要過濾掉所有結果,其中name應該包含“johar”。這可以使用SQLLIKE子句以及WHERE子句來處理。
If SQL LIKE clause is used along with % characters, then it will work like a meta character (*) in UNIX while listing out all the files or directories at command prompt.
Without a % character, LIKE clause is very similar to equals sign along with WHERE clause.
Syntax
Here is generic SQL syntax of SELECT command along with LIKE clause to fetch data from MySQLi table ?
SELECT field1, field2,...fieldN table_name1, table_name2...
WHERE field1 LIKE condition1 [AND [OR]] filed2 = "somevalue"
You can specify any condition using WHERE clause.
You can use LIKE clause along with WHERE clause.
You can use LIKE clause in place of equals sign.
When LIKE is used along with % sign then it will work like a meta character search.
You can specify more than one conditions using AND or OR operators.
A WHERE...LIKE clause can be used along with DELETE or UPDATE SQL command also to specify a condition.
Using LIKE clause at Command Prompt
這將使用SQL SELECT命令與WHERE ... LIKE子句從MySQLi表tutorials_inf中獲取所選數據。
例
以下示例將返回tutorials_inf表中的所有記錄,作者姓名以johar結尾,
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from tutorials_inf
-> WHERE name LIKE "%johar";
+----+-------+
| id | name |
+----+-------+
| 2 | johar |
+----+-------+
1 row in set (0.00 sec)
mysql>
在PHP腳本中使用LIKE子句
您可以將類似于WHERE ... LIKE子句的語法用于PHP函數mysqli_query()。此函數用于執行SQL命令,而后續的另一個PHP函數如果使用WHERE ... LIKE子句以及SELECT命令,則可以使用mysqli_fetch_array()來獲取所有選定的數據。
但是如果WHERE ... LIKE子句與DELETE或UPDATE命令一起使用,則不需要進一步調用PHP函數。
例
試試下面的例子返回所有記錄tutorials_inf對于其名稱中包含表喬哈爾-
$dbhost="localhost:3306";$dbuser="root";$dbpass="";$dbname="TUTORIALS";$conn=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);if(!$conn){die("Could not connect: ".mysqli_error());}echo"Connected successfully
";$sql="SELECT * FROM tutorials_inf WHERE name LIKE "%johar%"";$result=mysqli_query($conn,$sql);$row=mysqli_fetch_array($result,MYSQLI_ASSOC);printf("%s
",$row["name"]);mysqli_free_result($result);mysqli_close($conn);?>
樣本輸出應該是這樣的 -
Connected successfully
johar
總結
以上是生活随笔為你收集整理的mysql like ilike_MySQLilike 子句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql @@version_MySQ
- 下一篇: hibernate 无法保存timest