我有一个问题
select X from survey
where survey_date > date_add(curdate(), interval -90 day)它显示了哪些X是当前的。但不幸的是,在我无法控制的情况下,返回的值不是一个值列表,而是一个逗号分隔值列表。所以它会返回
| X |
+--------------------+
|thing1 |
|thing2,thing3,thing4|
|thing5 |
|thing6,thing7 |以此类推。
现在我想使用它作为一个子查询。如果数据是一对一的,我会这样做
select Y from othertable
where irrelevant_conditions && Y not in (
select X from survey
where survey_date > date_add(curdate(), interval -90 day)
);但这不起作用,因为我可能有,thing3说。
相反,我需要做RLIKE。(碰巧数据不能通过它们的形式冲突,所以这里不需要额外的regexp魔术来检查逗号。)这在MySQL中是可能的吗?
发布于 2012-05-04 03:04:14
要搜索逗号分隔的字符串,可以使用find_in_set。如果Y不是字符串,则执行隐式强制转换操作。
select Y from othertable
where irrelevant_conditions && not exists (
select 1
from survey
where
survey_date > date_add(curdate(), interval -90 day) and
find_in_set(othertable.Y, X) > 0
);发布于 2012-05-04 03:47:42
danhip的复杂查询可以简化为:
select Y from othertable
join survey on survey_date > date_add(curdate(), interval -90 day)
and find_in_set(othertable.Y, X) = 0
where irrelevant_conditionshttps://stackoverflow.com/questions/10437593
复制相似问题