2016-01-11 29 views
0

手动,我有这样的查询我的sql:你的SQL语法有错误;检查对应于你的MySQL服务器版本正确的语法使用近'

select c.*,j.pict 
from mst020 a 
inner join mst022 c on c.mst020_id = a.id 
    left join (select e.pict as pict from mst021 e 
where e.line_number = 
    (select max(f.line_number) from mst021 f where f.mst020_id = a.id) 
    and e.mst020_id = a.id) j 

但是当我处理该查询,,错误显示:

你的SQL语法有错误;检查对应于你的MySQL服务器版本的在线使用近“” 5

我试图因为甲骨文学会我的SQL它不是problme当G有子查询像that.thx正确的语法手册

+2

派生表必须haave一个别名:(从mst021 E选择e.pict为PICT其中e.line_number =(从mst021 f选择最大值(f.line_number)其中f.mst020_id = a.id)* * AS tmp ** –

+0

您需要在'left join'后使用表格 – Alex

+0

嗯我已经尝试过,但仍然错误.. –

回答

0

你错过了ON声明去与join的其余部分。 我已经重新格式化您的查询:

select c.*,j.pict 
from mst020 a 
inner join mst022 c on c.mst020_id = a.id 
left join (
       SELECT e.pict as pict 
       FROM mst021 e 
       WHERE e.line_number = 
           (
             SELECT max(f.line_number) 
             FROM mst021 f 
             WHERE f.mst020_id = a.id 
           ) 
        AND e.mst020_id = a.id 
     ) j 

正如你看到的,最后left join不具有on条款,最后and是错误的。另外,在最内层的查询中(select max),您可以引用最外层的查询。这不可能。你应该重写查询,至少,我不知道你在做什么,所以我不能纠正它。

+0

啊是thx。但仍然错误.. –

+0

请编辑您的问题以显示更正的查询和错误消息 – HoneyBadger

0

我有一个答案。因为我是错误的子查询,像这样

select c.*,j.pict 
from mst020 a 
inner join mst022 c on c.mst020_id = a.id 
    left join (select e.pict as pict from mst021 e 
where e.line_number = 
    (select max(f.line_number) from mst021 f where f.mst020_id = a.id) 
    and **e.mst020_id = a.id**) j on j.mst021_id = a.id 

外的子查询,所以我改变这个连接表和多数民众成功

select c.*,j.pict 
from mst020 a 
inner join mst022 c on c.mst020_id = a.id 
    left join (select e.pict as pict from mst021 e 
where e.line_number = 
    (select max(f.line_number),g.mst021_id from mst021 f)) j on j.mst021_id = a.id 

在我的SQL,我们不能用表连接在外部子查询

相关问题