Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 嵌套SQL语句以使用多个表中的字段创建Gridview_C#_Sql_Asp.net_Sql Server_Visual Studio 2013 - Fatal编程技术网

C# 嵌套SQL语句以使用多个表中的字段创建Gridview

C# 嵌套SQL语句以使用多个表中的字段创建Gridview,c#,sql,asp.net,sql-server,visual-studio-2013,C#,Sql,Asp.net,Sql Server,Visual Studio 2013,我有一个搜索页面,用户将在其中选择社区名称,按下搜索按钮后,将填充一个Gridview,显示该社区中所有帐户的所有者姓名、居民姓名和地址 我有以下表格:社区、地址、所有者、居民。Addresses具有CommunityID的外键。所有者和居民都有一个AddressID外键 使用SQL语句 SELECT Addresses.AddressID FROM Addresses INNER JOIN Communities ON Addresses.Communit

我有一个搜索页面,用户将在其中选择社区名称,按下搜索按钮后,将填充一个Gridview,显示该社区中所有帐户的所有者姓名、居民姓名和地址

我有以下表格:社区、地址、所有者、居民。Addresses具有CommunityID的外键。所有者和居民都有一个AddressID外键

使用SQL语句

SELECT 
     Addresses.AddressID 
FROM 
     Addresses 
     INNER JOIN Communities ON Addresses.CommunityID = Communities.CommunityID 
WHERE 
     Addresses.CommunityID = '1'    
我从第一个社区得到一个适当的AddressID列表。从这里我想列出属于每个AddressID的所有者、居民和实际地址

我觉得这样应该行得通:

SELECT        
     Owners.OwnLName, 
     Owners.OwnFName, 
     Residents.ResLName, 
     Residents.ResFName, 
     Addresses.Address
FROM            
     Addresses 
     INNER JOIN Owners ON Addresses.AddressID = Owners.AddressID 
     INNER JOIN Residents ON Owners.OwnerID = Residents.OwnerID 
WHERE 
     Owners.AddressID = ALL 
                        (
                           SELECT 
                               Addresses.AddressID 
                           FROM 
                               Addresses 
                               INNER JOIN Communities ON Addresses.CommunityID =Communities.CommunityID            
                               WHERE Addresses.CommunityID = '1'
                        )
我已经在这件事上纠结了这么长时间,似乎无法集中精力思考如何正确地表述这一点


任何帮助都将不胜感激

你可以这样做

SELECT        
     Owners.OwnLName, 
     Owners.OwnFName, 
     Residents.ResLName, 
     Residents.ResFName, 
     Addresses.Address
FROM            
     Addresses 
     INNER JOIN Owners ON Addresses.AddressID = Owners.AddressID 
     INNER JOIN Residents ON Owners.OwnerID = Residents.OwnerID 
WHERE 
     Owners.AddressID IN 
                        (
                           SELECT 
                               Addresses.AddressID 
                           FROM 
                               Addresses 
                               INNER JOIN Communities ON Addresses.CommunityID =Communities.CommunityID            
                           WHERE Addresses.CommunityID = '1'
                        ) 

这很有趣,因为使用这种语法,我得到一个返回的结果…其中AddressID和CommunityID正好是1。如果我将约束更改为CommunityID等于任何其他有效的CommunityID,则不会得到任何结果。所以它读这篇文章的时候,就好像我只想要AddressID和CommunityID相互匹配的指定字段..意思是说如果你改变了where Addresses.CommunityID='1'这个条件,那么你就不会得到结果?我直到现在才看到你的回答,我很抱歉。我这边有一个小错误,但你最初的帖子最终解决了我的问题。我不知道“IN”的用法。谢谢!