我有以下查询
SELECT Group_concat(employeename) AS name
FROM employee
WHERE Find_in_set(Find_in_set(employeeid,
(SELECT participantsids
FROM schedule
WHERE validfrom = '2016-04-21 17:00:00'
AND validto = '2016-04-21 17:30:00')
), '1,2');
这给我一个正确的价值..
但现在我必须检查
SELECT participantsids
FROM schedule
WHERE validfrom <= '2016-04-21 17:00:00'
AND validto >= '2016-04-21 17:30:00';
这会返回更多行,但我想在第一个查询中包含这个,所以我尝试了这个
SELECT Group_concat(employeename) AS name
FROM employee
WHERE Find_in_set(Find_in_set(employeeid,
(SELECT participantsids
FROM schedule
WHERE validfrom <= '2016-04-21 17:00:00'
AND validto >= '2016-04-21 17:30:00')
), '1,2');
这将返回错误“子查询返回超过1行”
我已尝试在子查询之前使用ANY和In,但它显示语法错误,我的问题是我应该在什么时候正确使用它们?
最佳答案 你可以尝试限制
SELECT participantsids
FROM schedule
WHERE validfrom <= '2016-04-21 17:00:00'
AND validto >= '2016-04-21 17:30:00'
LIMIT 1;
在你的情况下,我认为你以不正确的方式使用Find_in_set你应该使用这样的集合表示法
SELECT Group_concat(employeename) AS name
FROM employee
WHERE ( employeeid,
(SELECT participantsids
FROM schedule
WHERE validfrom <= '2016-04-21 17:00:00'
AND validto >= '2016-04-21 17:30:00'
LIMIT 1) = (1,2);