子查询返回多行

时间:2016-04-09 05:32:18

标签: mysql sql subquery

我有以下查询

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'); 

返回错误&#34;子查询返回多于1行&#34;

我尝试在子查询之前使用 ANY In ,但它显示语法错误,我的问题是我应该在什么时候正确使用它们?

1 个答案:

答案 0 :(得分:2)

您可以尝试限制

 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);