2013-01-16 64 views
-1

我已经在MySQL表中得到结果。 我只需要得到结果在user_id中只有6不是26而不是。mysql查询匹配确切字符串或数字

id users_id 

15 6,5 
16 1,5 
18 2,8 
19 1,26 
20 6,26 
21 5,10 
22 28,6 
23 21,9 
24 29,6 

我使用MySQL查询

SELECT * FROM WHERE users_id LIKE '%6%' 

结果

id user_id 

15 6,5 
19 1,26 why is this here.. 
20 6,26 
22 28,6 
24 29,6 

结果是不正确的。 我只需要:

id user_id 

15 6,5 
22 28,6 
24 29,6 
+0

对于像'%,6'这样的查询,如果表中有许多行,您可能会遇到性能问题。 –

回答

2

尝试::

SELECT * 
FROM tableName 
WHERE user_id like '6,%' or user_id like '%,6' or user_id like '%,6,%' 
0

,因为你的查询检查结果是否在每个地方包含6。

显然,26包含6 ...

你可以这样做(其他方式存在)

where (users_id like '6,%' or users_id like '%,6') 

sqlFiddle

+0

thanx它解决了 –

0

或者无需了解FIND_IN_SET或约 '或' 你只可以:

 
id users_id 

15 |6|5| 
16 |1|5| 
18 |2|6| 

,并选择user_ID的LIKE“%| 6 |% '