然后在表中存在返回更多行的内部联接

Sha*_*hah 5 t-sql sql-server

直到今天我对内连接的想法是它将返回满足连接条件的表中存在的最小行数.

防爆.如果表A包含4行而表B包含7行.我期待如果它们满足连接条件,则4行可以是最大输出.

我刚写了一篇sp,其中我正在创建两个临时表并填充它们.然后我采取了他们的内部联接但返回更多行(在我的情况下29行返回我期待4)经过一些搜索我发现这个 链接

这证实我可以发生,但我仍然想知道我有什么选择来限制返回的结果.

下面是我的存储过程.

ALTER PROCEDURE [dbo].[GetDDFDetailOnSiteCol]
@siteId int,
@colNum int
AS
BEGIN
SET NOCOUNT ON;

create Table #portDetail
(
ddfId int,
portDetail nvarchar(50),
siteId int
)
Insert into #portDetail SELECT  ddf.id,  ddf.portDetail, site.Site_ID  from site
        inner join ddf ON site.Site_ID = ddf.siteCodeID 
        where ddf.siteCodeID = @siteId and ddf.colNo= @colNum
        order by colNo,blockNum,portRowNum,portColNum

create Table #portAllocationDetail
(
assigned_slot nvarchar(50),
siteId int
)
Insert into #portAllocationDetail 
SELECT  dbo.portList.assigned_slot, dbo.site.Site_ID
FROM dbo.portList INNER JOIN
 dbo.site ON dbo.portList.siteCodeID = dbo.site.Site_ID
 where dbo.site.Site_ID = @siteId

--select * from #portAllocationDetail   
Select #portDetail.ddfId,#portDetail.portDetail,#portAllocationDetail.siteId,#portAllocationDetail.assigned_slot FROM #portDetail 
INNER JOIN #portAllocationDetail 
ON
#portDetail.siteId = #portAllocationDetail.siteId
END
Run Code Online (Sandbox Code Playgroud)

And*_*mar 14

一个inner join重复在表B每个匹配的行为TableA中的每一行.因此,如果TableA中有4行,而TableB中有7行,则最大行数为28.

SQL Fiddle的例子.