2012-02-24 34 views
1

查詢時:錯誤連接2臺

select i.Name,ri.Country,ri.State,ri.City 
from Information as i 
join ResidenceInformation as ri 
order by Name 

,我得到的是錯誤:

Error code -1, SQL state 42X01: Syntax error: Encountered "order" at line 4, column 5. 
Line 1, column 1 

Execution finished after 0 s, 1 error(s) occurred. 

爲什麼我會得到一個錯誤?

+0

netbeans自帶的derbi – 2012-02-24 03:17:20

回答

4

這個錯誤是因爲你忘了指定連接標準,如:

SELECT i.Name, ri.Country, ri.State, ri.City 
    FROM Information as i 
    JOIN ResidenceInformation as ri ON ri.column = i.column 
ORDER BY Name 

您需要與正確鏈接表您需要的輸出相應列的名稱,以取代column

您還應該在ORDER BY中指定表別名,以防止模糊的列引用錯誤。

1

,因爲你的語法不正確,你得到一個錯誤:join後,需要有on,就像這樣:

select i.Name,ri.Country,ri.State,ri.City 
from Information as i 
join ResidenceInformation as ri 
    on ri.info_id=i.id -- <<< Added a join condition 
order by Name 

SQL需要知道如何「連接起來」的表中的行,你是加入查詢中其他表格的行。我假設ResidenceInformation有一個外號爲Informationinfo_id

如果Name存在於ResidenceInformationInformation,您需要使用表名或別名前綴它。事實上,無論如何,爲了增加清晰度,這是一個好主意。

0

我想你可能忘了告訴加入條款加入哪些列。你需要告訴數據庫這兩個表是如何相互連接的。像ON i.id = ri.InformationId

select i.Name,ri.Country,ri.State,ri.City 
from Information as i 
join ResidenceInformation as ri ON i.id = ri.InformationId 
order by i.Name 

同樣的東西,你可能需要order by子句,我已經添加,以及在表的別名。