MySQL LIKE 查詢帶動態陣列?


要實現帶有動態陣列的 LIKE 查詢,語法如下 –

示例

select *from yourTableName
   where yourColumnName2 like "%yourValue%"
   order by yourColumnName1 asc
   limit yourLimitValue;

讓我們建立一個表 –

示例

mysql> create table demo74
   -> (
   -> user_id int not null auto_increment primary key,
   -> user_names varchar(250)
   -> )
   -> ;
Query OK, 0 rows affected (0.67

藉助 insert 命令向表中插入一些記錄 –

示例

mysql> insert into demo74(user_names) values("John Smith1,John Smith2,John Smith3");
Query OK, 1 row affected (0.18

mysql> insert into demo74(user_names) values("John Smith1");
Query OK, 1 row affected (0.15

mysql> insert into demo74(user_names) values("David Smith1");
Query OK, 1 row affected (0.12

mysql> insert into demo74(user_names) values("John Smith1,John Smith2,John Smith3,John Smith4");
Query OK, 1 row affected (0.10

使用 select 語句從表中顯示記錄 –

示例

mysql> select *from demo74;

這將生成以下輸出 –

輸出

+---------+-------------------------------------------------+

| user_id | user_names                                      |

+---------+-------------------------------------------------+

|       1 | John Smith1,John Smith2,John Smith3             |

|       2 | John Smith1                                     |

|       3 | David Smith1                                    |

|       4 | John Smith1,John Smith2,John Smith3,John Smith4 |

+---------+-------------------------------------------------+

以下是帶有動態陣列的 MySQL LIKE 查詢 –

示例

mysql> select *from demo74
-> where user_names like "%John Smith1%"
-> order by user_id asc
-> limit 100;

這將生成以下輸出 –

輸出

+---------+-------------------------------------------------+

| user_id | user_names                                      |

+---------+-------------------------------------------------+

|       1 | John Smith1,John Smith2,John Smith3             |

|       2 | John Smith1                                     |

|       4 | John Smith1,John Smith2,John Smith3,John Smith4 |

+---------+-------------------------------------------------+

3 rows in set (0.00 sec)

更新於:11-12-2020

1K+ 次觀看

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.