连接两个子查询别名

时间:2018-08-09 16:44:31

标签: mysql join subquery

我已经在这里找到了许多类似的请求,但是不幸的是没有什么是一样的。我尝试链接两个子查询,但是以某种方式无法正常工作。这是我的代码:

SELECT a_id, a_sid, a_pid, a_trait1, a_trait2, a_trait3 FROM 
(SELECT 
id AS a_id, sid AS a_sid, pid AS a_pid, max(trait1) AS a_trait1, trait2 AS a_trait2, trait3 AS a_trait3 
FROM 
table_a 
GROUP BY 
sid) AS derived_a, 
(SELECT 
id AS s_id, trait10 AS s_trait10, trait11 AS s_trait11, trait12 AS s_trait12 
FROM 
table_s) AS derived_s 
LEFT JOIN 
derived_a ON derived_a.a_sid = derived_s.s_id

1 个答案:

答案 0 :(得分:1)

错误的sintax,您应该对子查询使用左连接,而不要使用别名

  SELECT a_id, a_sid, a_pid, a_trait1, a_trait2, a_trait3 
  FROM  ( 
      SELECT  id AS a_id, sid AS a_sid, pid AS a_pid
        , max(trait1) AS a_trait1, trait2 AS a_trait2, trait3 AS a_trait3 
      FROM  table_a 
      GROUP BY id
  ) AS derived_a 
  left JOIN  (
      SELECT id AS s_id, trait10 AS s_trait10
        , trait11 AS s_trait11, trait12 AS s_trait12 
      FROM table_s) AS derived_s  ON derived_a.a_sid = derived_s.s_id