关于查询的一个问题
某表记录是这样的
id title cid
100 张三 2,3
101 李四 3,4
102 王五 12,13
如果搜索,条件是要查cid字段=2的记录
应该怎么写?
sql语句里的like应该不严谨,可能会把100和102这两条记录都查出来
[解决办法]
SELECT * FROM table WHERE FIND_IN_SET('2',cid);
[解决办法]
mysql> select find_in_set(2, "2,3,4");
+-------------------------+
| find_in_set(2, "2,3,4") |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select find_in_set(2, "52,3,4");
+--------------------------+
| find_in_set(2, "52,3,4") |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
mysql> select find_in_set(2, "2,3,4");
+-------------------------+
| find_in_set(2, "2,3,4") |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)