-1

I have result in MySQL table. I need to get result only where is only 6 not 26 in user_id not.

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

i am using MySQL query

SELECT * FROM WHERE users_id LIKE '%6%'

Result

id  user_id

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

result is not correct. i need only :

id  user_id

15  6,5
22  28,6
24  29,6
4

5 回答 5

2

Try::

SELECT  *
FROM    tableName
WHERE   user_id like '6,%' or user_id like '%,6' or user_id like '%,6,%'
于 2013-01-16T09:59:49.453 回答
1
SELECT  *
FROM    tableName
WHERE   FIND_IN_SET(6, users_id) > 0 AND
        FIND_IN_SET(26, users_id) = 0

SOURCE

于 2013-01-16T09:58:30.887 回答
0

because your query checks if the result contains a 6 at every place.

Obviously, 26 contains 6...

You could do (other ways exist)

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

sqlFiddle

于 2013-01-16T09:59:04.573 回答
0

maybe try:

SELECT * FROM WHERE users_id IN ('6');

should just match the 6.

http://www.w3schools.com/sql/sql_in.asp

于 2013-01-16T10:16:31.807 回答
0

Or without knowing about FIND_IN_SET or about 'OR' you just could:

id   users_id

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

and select user_id like '%|6|%'

于 2013-04-20T18:57:55.943 回答