2015-11-30 105 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”