【发布时间】:2011-02-10 03:02:59
【问题描述】:
我刚刚遇到了一个问题。
我知道这些整数,$integers: 3,5,15,20。
我只想从下表中选择在字段 NUMBERS 中找到所有逗号分隔的 INT 的行。
TABLE: number_table
Uid Numbers
------------------------
1 3,5,15 OK, since all of NUMBERS are in $integers
2 5,15,20 OK, since all of NUMBERS are in $integers
3 3,4,5,15 NOT OK, since 4 is not found in $integers
4 2,15,20,25 NOT OK, since 2 and 25 is not found in $integers
是否可以对逗号分隔的字符串执行“for-each”或以其他方式执行此 SELECT?
更新:听起来这是不可能的。我会把它留在这里一会儿。只是一个提示。当在逗号分隔的字符串中搜索某些内容时,MySQL 会提供 WHEERE something IN(逗号分隔的字符串)。我所寻找的是以某种方式使用 MySQL 遍历逗号分隔的字符串,但这可能是不可能的。
这样的事情会做(伪代码):
SELECT * FROM number_table WHERE each_commaseparated_substring(Numbers , 'IN (3,5,15,20)')
【问题讨论】: