开发者

MySQL IN() strange behavior

If i do a select like this:

SELECT `id` FROM `table` WHERE `id` IN (1,2,3)

it works but if i do like this:

SELECT `id` FROM `table`
WHERE `id` IN (`column`)

where column is an internal colum开发者_如何转开发n with values: 1,2,3 it returns only 1

This is the query:

SELECT a.id, (
   SELECT f.name FROM facs f WHERE f.id IN(REPLACE(a.facs, ';', ','))
) AS facs
FROM ads a LIMIT 0, 10

Thanks


I think instead of

SELECT `id` FROM `table`
WHERE `id` IN (`column`)

You should do :

SELECT `id` FROM `table`
WHERE `id` IN (SELECT `column` FROM `table`)


You can use the FIND_IN_SET() function:

SELECT a.id, (
   SELECT f.name
   FROM facs f
   WHERE FIND_IN_SET(f.id, REPLACE(a.facs, ';', ','))
) AS facs
FROM ads LIMIT 0, 10

Note: Since this is using a function, indexes cannot be used, you should think about storing the facs in multiple rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜