1

我无法将变量传递给子查询。

我有两张不同的桌子,需要所有面试人员。

当前我的 SQL

SELECT
   empl.id AS id,
   (SELECT
         GROUP_CONCAT(interviewed_by SEPARATOR ', ') 
      FROM
         (
            SELECT
               interview_old.interviewed_by 
            FROM
               interview_old 
            WHERE
               interview_old.empl = empl.id 

            UNION

            SELECT
               interview.interviewed_by 
            FROM
               interview
            WHERE
               interview.empl = empl.id 
         )
         as interviewed_by 
   ) AS interviews
FROM
   empl AS empl 

这不是我的完整代码,所以我无法更改这部分

SELECT
   empl.id AS id,
   {only here allow insert custom sql}
FROM
   empl AS empl 
4

2 回答 2

1

您的问题不是 100% 清楚,但您可以首先将您的查询转换为不使用子查询,而只LEFT JOIN使用GROUP

SELECT
   empl.id AS id,
   GROUP_CONCAT(i.interviewed_by SEPARATOR ', ')
FROM
   empl AS empl 
LEFT JOIN (
   SELECT empl, interviewed_by
   FROM interview_old
   UNION
   SELECT empl, interviewed_by 
   FROM interview
) i
ON i.empl = empl.id 
GROUP BY empl.id
于 2017-04-03T14:12:57.623 回答
1

您需要从子查询中获得所需的结果,然后将整个事物与主表 empl 连接,然后按 empid 分组。

   SELECT
       empl.id AS id, GROUP_CONCAT(interviewed_by SEPARATOR ', ') 
        FROM
             (SELECT interview_old.empl employeeid, interview_old.interviewed_by as interviewed_by FROM interview_old
                    UNION
            SELECT  interview.empl,interview.interviewed_by 
                  FROM interview ) as tmp join   empl AS empl  on (empl.id=tmp.employeeid) 
                  group by  empl.id
于 2017-04-03T14:15:40.503 回答