0

我有一个数据库驱动的 webapp。在我的数据库中,我有四个表:

User
----
UserID
FirstName
LastName

UserGroup
---------
UserGroupID
UserID
GroupID

Group
-----
GroupID
Name

Adress
------
AdressID
UserID
Street
...

使用我当前的 SQL 语句,我没有得到任何返回。这是我的 SQL:

select 
    a.UserID, a.FirstName, a.LastName, b.GroupID, b.UserID, 
    c.GroupID, c.Name, d.AdressID, d.UserID, 
from User as a, UserGroup as b, Group as c, Adress as d 
where a.UserID = b.UserID and b.GroupID = c.GroupID 
    and a.UserID = d.UserID and d.AdressID= @id"

我为变量 id 提供以下语句:

dbCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;

知道为什么我什么都没退回吗?我也没有任何错误。我DataTable在调试时将结果存储为空。

4

2 回答 2

3

你在'from'之前有逗号,而且你最好使用inner join而不是像这样加入:

select a.UserID, a.FirstName, a.LastName, b.GroupID, b.UserID, c.GroupID, c.Name, d.AdressID, d.UserID
from User as a
inner join UserGroup as b on b.UserId = a.UserId
inner join Group as c on c.GroupID = b.GroupID
inner join Adress as d on a.UserID = d.UserID
where d.AdressID = @id

试试这个。

于 2013-06-11T13:00:11.533 回答
1

尝试INNER JOIN查询

SELECT a.UserID, a.FirstName, a.LastName, b.GroupID, b.UserID, c.GroupID, c.Name, d.AdressID, d.UserID
FROM User as a
INNER JOIN UserGroup as b 
ON a.UserID = b.UserID 
INNER JOIN Group as c 
ON b.GroupID = c.GroupID
INNER JOIN Adress as d 
ON a.UserID = d.UserID
WHERE d.AdressID= @id
于 2013-06-11T13:00:37.017 回答