SQL SQL在内部连接中间使用嵌套选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3679493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL use nested select in middle of inner join
提问by user380432
Is it possible to use a select in the middle of joining...
是否可以在加入过程中使用选择...
I am trying to do the following:
我正在尝试执行以下操作:
FROM
tblorders o
INNER JOIN tblunits u on o.id = u.orderid
INNER JOIN ((SELECT
,Min(n.date) as [MinDate]
from tblNotes n
Where n.test = 'test') te
INNER JOIN tblnotes n on te.id = n.id
and te.[MinDate] = n.AuditinsertTimestamp)
INNER Join tblClient c ON o.ClientId = c.Id
Basically in the select in the middle of the query it is selecting only the notes with min date. The problem is I need to do this here because I need from tblOrders to be the first table.......
基本上在查询中间的选择中,它只选择具有最小日期的笔记。问题是我需要在这里这样做,因为我需要从 tblOrders 成为第一个表......
Suggestions?
建议?
回答by RedFilter
The INNER JOIN failed because you have a leading comma here:
INNER JOIN 失败,因为这里有一个前导逗号:
,Min(n.date) as [MinDate]
I think you are looking for something like this:
我想你正在寻找这样的东西:
SELECT ...
FROM tblorders o
INNER JOIN tblunits u on o.id = u.orderid
INNER JOIN (
SELECT id, Min(date) as [MinDate]
from tblNotes
Where test = 'test'
group by id
) te <-- not sure what JOIN clause to use here, please post schema
INNER JOIN tblnotes n on te.id = n.id
and te.[MinDate] = n.AuditinsertTimestamp
INNER Join tblClient c ON o.ClientId = c.Id
回答by A-K
You are missing an alias and join condition:
您缺少别名和连接条件:
FROM
tblorders o
INNER JOIN tblunits u on o.id = u.orderid
INNER JOIN ((SELECT Min(n.date) as [MinDate]
from tblNotes n
Where n.test = 'test') te
INNER JOIN tblnotes n on te.id = n.id
and te.[MinDate] = n.AuditinsertTimestamp)
-- missing
AS z
ON <join conditions haere>
INNER Join tblClient c ON o.ClientId = c.Id
回答by Sidharth Panwar
Yes, you can have a Select in a Join.
是的,您可以在加入中进行选择。