RLIKE带子查询?

时间:2012-05-03 18:52:47

标签: mysql sql regex

我有一个查询

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。 (实际上数据不会因其形式发生冲突,因此这里不需要额外的正则表达式来检查逗号。)这在MySQL中可能吗?

2 个答案:

答案 0 :(得分:2)

要搜索以逗号分隔的字符串,您可以使用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 
);

答案 1 :(得分:0)

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_conditions