MySQL 子选择问题 [英] MySQL Subselect issue
本文介绍了MySQL 子选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 mysql 子选择有问题.
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')
我只得到两个文件中的一个(ID 为 7).我在这里错过了什么?
I only get one of the two files (with the id 7 that is). What am I missing here?
推荐答案
对于具有串联数据的列(如您的文章"列),您不能使用 MySQL IN() 函数.而是使用字符串函数 FIND_IN_SET()
来查询这些值.在你的情况下:
For a column with concatenated data (like your "articles" column), you can not use MySQL IN() Function. Instead use the string function FIND_IN_SET()
to query such values. In your case:
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
A working sqlfiddle: http://sqlfiddle.com/#!2/796998/3/0
这篇关于MySQL 子选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文