Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
基本SQL查询问题_Sql_Sql Server 2005 - Fatal编程技术网

基本SQL查询问题

基本SQL查询问题,sql,sql-server-2005,Sql,Sql Server 2005,我有以下疑问: SELECT Base.ReportDate, Base.PropertyCode, Base.FirstName, Base.MiddleName, Base.LastName, Person.FirstName, Person.MiddleName, Person.LastName FROM LeaseT INNER JOIN Base ON LeaseT.LeaseID = Base.LeaseID INNER

我有以下疑问:

SELECT     
  Base.ReportDate, 
  Base.PropertyCode, 
  Base.FirstName, 
  Base.MiddleName, 
  Base.LastName, 
  Person.FirstName, 
  Person.MiddleName, 
  Person.LastName
FROM  LeaseT INNER JOIN
   Base ON LeaseT.LeaseID = Base.LeaseID INNER JOIN
   Person ON LeaseT.TenantID = Person.ID
工作正常,只是对于基表中的每个记录,“Person”表中可能有0到“N”个人,但我只想为每个“Base”记录返回1(哪一个不重要,但Person.ID最低的记录)是一个合理的选择。如果person表中有0行,我仍然需要返回该行,但“person”字段的值为空

我将如何构造SQL来实现这一点

谢谢


编辑:是的,表的结构可能不正确,但此时无法重新构造-请使用现有的内容。

使用左连接

SELECT     
  Base.ReportDate, 
  Base.PropertyCode, 
  Base.FirstName, 
  Base.MiddleName, 
  Base.LastName, 
  d.FirstName, ou

  d.MiddleName, 
  d.LastName
FROM  LeaseT INNER JOIN
   Base ON LeaseT.LeaseID = Base.LeaseID INNER JOIN
left outer join   
 (select min(personid) as ID from person group by personid) as d 
on 
 LeaseT.TenantID = d.ID 
left outer join 
 (select FirstName, 
  MiddleName, 
  LastName from person) d1
on
  LeaseT.TenantID = d1.ID 

以下内容可能对您有所帮助

SELECT     
  Base.ReportDate, 
  Base.PropertyCode, 
  Base.FirstName, 
  Base.MiddleName, 
  Base.LastName, 
  d.FirstName, ou

  d.MiddleName, 
  d.LastName
FROM  LeaseT INNER JOIN
   Base ON LeaseT.LeaseID = Base.LeaseID INNER JOIN
left outer join   
 (select min(personid) as ID from person group by personid) as d 
on 
 LeaseT.TenantID = d.ID 
left outer join 
 (select FirstName, 
  MiddleName, 
  LastName from person) d1
on
  LeaseT.TenantID = d1.ID 
这行吗

SELECT     
  Base.ReportDate, 
  Base.PropertyCode, 
  Base.FirstName, 
  Base.MiddleName, 
  Base.LastName, 
  Person.FirstName, 
  Person.MiddleName, 
  Person.LastName
FROM  Base
LEFT JOIN (
    SELECT LeaseID, MIN(TenantID) AS [TenantID]
    FROM LeaseT
    GROUP BY LeaseID) AS [LeaseT_SinglePerson] ON Base.LeaseID = [LeaseT_SinglePerson].LeaseID
LEFT JOIN Person ON [LeaseT_SinglePerson].TenantID = Person.ID
这行吗

SELECT     
  Base.ReportDate, 
  Base.PropertyCode, 
  Base.FirstName, 
  Base.MiddleName, 
  Base.LastName, 
  Person.FirstName, 
  Person.MiddleName, 
  Person.LastName
FROM  Base
LEFT JOIN (
    SELECT LeaseID, MIN(TenantID) AS [TenantID]
    FROM LeaseT
    GROUP BY LeaseID) AS [LeaseT_SinglePerson] ON Base.LeaseID = [LeaseT_SinglePerson].LeaseID
LEFT JOIN Person ON [LeaseT_SinglePerson].TenantID = Person.ID

什么数据库?这又是“组中的最大值”,除了你想要的最小值而不是最大值。你到底想做什么,只是从每个基数中随机返回一个人?按照您现在的方式,我认为无论您做什么,它都会返回每个人,因为person字段位于select.what数据库中?这又是“组中的最大值”,除了你想要的最小值而不是最大值。你到底想做什么,只是从每个基数中随机返回一个人?你现在的样子,我想无论你做什么,它都会返回每个人,因为person字段在select中。我想LeaseT应该是左连接的,因为它是Base和Personsorry之间的连接,但我不理解你,我想LeaseT应该是左连接的,因为它是Base和Personsorry之间的连接,但我是不明白