如何在 MySQL 中選擇特定行?
如需選擇特定行,請在 MySQL 中使用 FIND_IN_SET() 函式。我們首先建立一張表 -
mysql> create table DemoTable ( ListOfValues varchar(200) ); Query OK, 0 rows affected (0.31 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable values('112,114,567,Java,345'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values('222,214,256'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values('2,567,98,C'); Query OK, 1 row affected (0.06 sec)
使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
將生成以下輸出 -
+----------------------+ | ListOfValues | +----------------------+ | 112,114,567,Java,345 | | 222,214,256 | | 2,567,98,C | +----------------------+ 3 rows in set (0.00 sec)
以下是 MySQL 中選擇特定行的查詢 -
mysql> select *from DemoTable where find_in_set('2',ListOfValues) >0;
將生成以下輸出 -
+--------------+ | ListOfValues | +--------------+ | 2,567,98,C | +--------------+ 1 row in set (0.03 sec)
廣告