2015-11-30 106 views
-1

我想獲取People和地址的所有列,其中CategoryId等於類別。sql查詢連接多個表

有此查詢。

SELECT 
    Category.Id, People.*, Adress.* 
FROM  
    Category 
JOIN 
    People ON People.CategoryId = Category.CategoryId 
JOIN  
    Adress ON Adress.PeopleId = People.Id 
WHERE  
    Category.CategoryId = @category 
ORDER BY 
    People.PeopleId DESC ; 

當我運行我的應用程序時出現錯誤。

Server Error in '/' Application.

Invalid column name 'Id'. Invalid column name 'Id'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'Id'. Invalid column name 'Id'.

Source Error: Line 153: da.SelectCommand = command; Line 154: DataTable dt = new DataTable(); Line 155:
da.Fill(dt); Line 156: cn.Close(); Line 157:
PagedDataSource pgitems = new PagedDataSource();

爲什麼它不起作用?一些幫助 ?

使用visual studio本地數據庫。

+0

什麼是不工作?它在語法上是不正確的嗎?你會得到意想不到的結果?請說明 – andrewdleach

+0

運行時會發生什麼? –

+0

哪種RDBMS適用於?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

回答

0

更改編號到相應的類別編號和的peopleid

SELECT Category.CategoryId , People.*, Adress.* 
FROM  Category JOIN People ON People.CategoryId=Category.CategoryId 
JOIN  Adress ON Adress.PeopleId=People.PeopleId 
WHERE [email protected] 
ORDER BY People.PeopleId DESC ; 
+0

謝謝你幫助我。我也有約束關鍵問題。 但現在它完美:)) – n00bie

1

錯誤是因爲您的表「類別」沒有列名「Id」。看起來你需要將它改爲「CategoryId」。

您的人員表格中也可能有類似的問題(無「Id」列)。

+0

類別具有名爲CategoryId的列。 – n00bie

+1

對 - 爲什麼你要在代碼中引用「Category.Id」? – Russ

0

該錯誤似乎是在Category.Id,驗證是否正確與這個大寫。我想,這大概是「Category.id」或「Category.CategoryId」