MySQL 列表 IN 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8352115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL list IN list
提问by Sir
I currently store user's inputs in comma separated lists like so:
我目前将用户的输入存储在逗号分隔的列表中,如下所示:
Userid | Options
1 | 1,2,5
A user ticks a set of options in a form which is an array, which is then joined with a comma to make
用户以数组形式勾选一组选项,然后用逗号将其连接起来
1,2,5
Then MySQL is trying to find other users who some or all of the same options ticked on a different field name (although same table).
然后 MySQL 试图找到其他用户,他们在不同的字段名称(尽管同一个表)上勾选了一些或所有相同的选项。
Currently I do this:
目前我这样做:
WHERE `choices` IN ('.$row['myoptions'].')
So this could be something like:
所以这可能是这样的:
WHERE 1,2,5,8 IN (1,4,6)
This would return true because theres at least one value match right? Or have i got this confused..
这将返回 true,因为至少有一个值匹配对吗?或者我有这个困惑..
回答by Starx
May be you are going the wrong way to do this.
可能是你做错了。
The function FIND_IN_SET might be helpful if the options column type is SET
.
如果选项列类型为 ,则函数 FIND_IN_SET 可能会有所帮助SET
。
Example:
例子:
SELECT * FROM yourtabe WHERE FIND_IN_SET('2', Options);
But, it will only let you compare one string at a time, in the above example, it compares if 2 is present in the rows. If you have to compare multiple values you cannot accomplish that by using FIND_IN_SET.
但是,它一次只能让您比较一个字符串,在上面的示例中,它比较行中是否存在 2。如果您必须比较多个值,则无法通过使用 FIND_IN_SET 来实现。
However, in your case, LIKE
clause may be of use to.
但是,在您的情况下,LIKE
子句可能有用。
May be something like
可能是这样的
SELECT * FROM yourtable WHERE Options LIKE '%2,3%';
Now this will search for 2,3 value anywhere in the column, and give the result. But this also comes with another complication, it gives the result only if 2,3
is present side by side of each other, if the column has 2,1,3
or 2,4,5,3
or even 3,2
it will not list these records.
现在这将在列中的任何位置搜索 2,3 值,并给出结果。但是,这也带有另一个并发症,它给仅当结果2,3
是彼此并排存在,如果列2,1,3
或者2,4,5,3
甚至3,2
它不会列出这些记录。
Now coming to your question
现在来回答你的问题
`WHERE `choices` IN (1,4,6)`,
will translate to
将翻译成
WHERE `choices` = '1' OR `choices` = '4' OR `choices` = '6'
so it will return false
所以它会回来 false
Why?
为什么?
because your column contains not only 1 or 4 or 6 but 1,2,5
as one string. So all the comparisons above to return false
因为您的列不仅包含 1 或 4 或 6,而且1,2,5
包含一个字符串。所以上面的所有比较都返回false
回答by Zohaib
I do not think this will return true.
我不认为这会返回 true。
WHERE CHOICES IN ()
when you do this, it will compare complete choices
value to individual item inside IN
当你这样做时,它会将完整的choices
价值与里面的单个项目进行比较IN
You might wanna have a look at find_in_Set
function of MySQL
你可能想看看find_in_Set
MySQL 的功能
WHERE find_in_set(optionNumber1, choices) > 0
OR find_in_set(optionNumber2, choices) > 0
OR find_in_set(optionNumber3, choices) > 0
You will have to make query in a loop in programming language you are using
您必须使用您正在使用的编程语言在循环中进行查询
回答by ScoRpion
I think you are not getting Confused. You are absolutely right this will return something (a tuple or more then one tuple) and that of-course is a True value. So Carry on....
我认为你不会感到困惑。你是绝对正确的,这将返回一些东西(一个元组或多个然后一个元组),当然这是一个 True 值。所以继续……