2016-01-11 31 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,我們不能用表連接在外部子查詢

相關問題