0

I have an issue with a mysql subselect.

**token table:**
id | token | articles
1  | 12345 | 7,6
2  | 45saf | 6,7,8

**items table:**
id | name                | filename
6  | Some brilliant name | /test/something_useful.mp3
7  | homer simpson       | /test/good-voice.mp3

**query:**
SELECT items.`filename`,items.`name` FROM rm_shop items WHERE items.`id` IN ( SELECT token.`articles` FROM rm_token token WHERE token.`token` = 'token')

I only get one of the two files (with the id 7 that is). What am I missing here?

4

1 回答 1

1

对于具有串联数据的列(例如您的“文章”列),您不能使用 MySQL IN() 函数。而是使用字符串函数FIND_IN_SET()来查询这些值。在你的情况下:

SELECT items.`filename`,items.`name` FROM rm_shop items
WHERE FIND_IN_SET(items.`id`,
(SELECT token.`articles` FROM rm_token token WHERE token.`token` = 'token')) > 0

一个工作的 sqlfiddle:http ://sqlfiddle.com/#!2/796998/3/0

于 2013-06-09T10:06:28.887 回答