结合内连接和左连接的结果

时间:2013-03-21 07:14:00

标签: mysql

SELECT name,trans FROM skyplan_deploy.deploy_sids d WHERE apt='KBOS' AND name != trans  
LEFT JOIN  
(SELECT distinct c.sid_ident as name,c.fix_ident from corept.std_sid_leg as c  
   INNER JOIN  
        (SELECT sid_ident,transition_ident,max(sequence_num) seq,route_type  
         FROM corept.std_sid_leg  
         WHERE data_supplier='J' AND airport_ident='KBOS'  
         GROUP BY sid_ident,transition_ident)b  
         ON c.sequence_num=b.seq and c.sid_ident=b.sid_ident and        c.transition_ident=b.transition_ident  
         WHERE c.data_supplier='J' and c.airport_ident='KBOS')right_tbl  
ON d.name=right_tbl.sid_ident;

这是我的代码..当执行时我在左连接时收到错误说法语错误。有人请帮助...我已经通过语法教程但最后空手而归。谢谢。

1 个答案:

答案 0 :(得分:4)

WHERE子句移动到查询的末尾。另外,trans子句中的列WHERE是什么?它从哪里来的?如果它是一个字符串文字,那么把它放在引号中。

应该这样写:

SELECT 
  name,
  trans 
FROM skyplan_deploy.deploy_sids d 
LEFT JOIN
(
   SELECT distinct c.sid_ident as name, c.fix_ident 
   from corept.std_sid_leg as c
   INNER JOIN
   (
      SELECT sid_ident, transition_ident, max(sequence_num) seq, route_type
      FROM corept.std_sid_leg
      WHERE data_supplier='J' AND airport_ident='KBOS'
      GROUP BY sid_ident,transition_ident
   ) b ON c.sequence_num=b.seq 
       and c.sid_ident = b.sid_ident 
       and c.transition_ident = b.transition_ident
    WHERE c.data_supplier='J' and c.airport_ident='KBOS'
) AS right_tbl  ON d.name = right_tbl.sid_ident
WHERE apt = 'KBOS'
  AND right_tbl.sid_ident IS NULL ;