0

我有一个查询

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 中可能吗?

4

2 回答 2

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 
);
于 2012-05-03T19:04:13.593 回答
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
于 2012-05-03T19:47:42.140 回答