需要多个内部联接来检查行是否存在? - SQL 服务器

Multiple Inner Join needed to check If rows exist? - SQL Server

我已经重新提交了这个问题,因为它有错误,我相信我现在已经修正了,抱歉。

好的,下面的存储过程工作正常,但我需要添加与另一个名为 tblItems 的 table 相关的条件。我要求 tblAccounts.accountID 加入 tblItems.AccountID,这样我就可以检查是否 tblItems.fileID > 0,如果是,则 return 结果。这是我的 tables 和预期的结果..

TBLACCOUNTS: AccountID, ContactName, SkypeUserName, friendlyOrderID

TBLORDERS: AccountID, ItemsAllowed

TBLITEMS: AccountID, FileID, ImageUrl

我希望达到的是这个..

4B900A74-E2D9-4837-B9A4-9E828752716E  PETER     PETE  827365
E82882D9-4837-B9A4-9E82-22228752716E  MATTHEW   MATT  373926
4B900A74-2323-1414-2525235252533333D  JONATHAN  JON   732792...

这就是下面第一个代码块的作用。 但我只需要每个用户在 tblItems 中至少有 1 个文件 ID 的行。当我尝试下面的第二个代码块时得到的结果如下..

4B900A74-E2D9-4837-B9A4-9E828752716E  PETER     PETE  827365
4B900A74-E2D9-4837-B9A4-9E828752716E  PETER     PETE  827365
4B900A74-E2D9-4837-B9A4-9E828752716E  PETER     PETE  827365...

代码块1

SELECT 
    *,
    CASE WHEN RowNo < cnt THEN 'N' ELSE 'Y' END AS lastbox
FROM
    (SELECT 
         ROW_NUMBER() OVER (ORDER BY dateAdded DESC) AS [RowNo],
         COUNT(*) OVER () cnt,
         tblAccounts.skypeUserName, tblAccounts.contactName, 
         tblorders.friendlyOrderID
     FROM 
         tblOrders
     INNER JOIN 
         tblAccounts ON tblOrders.accountID = tblAccounts.accountID
     WHERE 
         bootSaleDate = @bootSaleDate AND orderStatus = 'Completed') t
WHERE 
    RowNo BETWEEN (@page * 8) - 7 AND (@page * 8)

这是我试过的 (Codeblock2)..

SELECT *,
CASE WHEN RowNo < cnt THEN 'N' ELSE 'Y' END AS lastbox
    FROM
    (
    SELECT ROW_NUMBER() OVER (ORDER BY orderDate desc) as [RowNo],
    COUNT(*) OVER () cnt,
    tblAccounts.skypeUserName, tblAccounts.contactName, tblorders.friendlyOrderID
    FROM tblOrders
    INNER JOIN tblAccounts ON tblOrders.accountID=tblAccounts.accountID
    INNER JOIN tblItems ON tblOrders.accountID=tblItems.accountID
    WHERE EXISTS(select 1 from tblItems WHERE tblItems.fileID > 0 AND tblItems.accountID = 
    tblOrders.accountID AND tblOrders.bootSaleDate = @bootSaleDate)  AND tblOrders.bootSaleDate = 
    @bootSaleDate AND tblOrders.orderStatus='Completed' 
    ) t
WHERE RowNo BETWEEN (@page*8)-7 AND (@page*8)

也许你的意思是这样的,其中子查询计算出 fileid > 0

的 tblitems 存在多少行
 drop table tblorders
 drop table tblaccounts
 drop table tblitems
 go

 create table tblorders (accountid int,orderdate smalldatetime,friendlyorderid int,
                bootsaledate smalldatetime, orderstatus varchar(10))
 create table tblaccounts(accountid int,skypeusername varchar(3),contactname varchar(3))
 create table tblitems(accountid int,fileid int)
 insert into tblorders values
 (1,'2020-04-29',1,'2020-04-29','completed'),
 (2,'2020-04-29',1,'2020-04-29','completed')

 insert into tblaccounts values
 (1,'aaa','aaa'),
 (2,'bbb','bbb')

 truncate table tblitems
 insert into tblitems values
 (1,0),(2,2),(1,1)
 declare @bootsaledate smalldatetime
 set @bootsaledate = '2020-04-29'
 SELECT tblorders.accountid,tblaccounts.accountid,s.accountid,
        ROW_NUMBER() OVER (ORDER BY orderDate desc) as [RowNo],
    COUNT(*) OVER () cnt,
    tblAccounts.skypeUserName, tblAccounts.contactName, tblorders.friendlyOrderID
    FROM tblOrders
    INNER JOIN tblAccounts ON tblOrders.accountID=tblAccounts.accountID
    INNER JOIN 
        (select accountid,sum(case when fileid > 0 then 1 else 0 end) fileid 
        from tblitems
        group by accountid) s  ON tblOrders.accountID=s.accountID
    WHERE s.fileID > 0 
    AND tblOrders.bootSaleDate = @bootSaleDate 
    AND tblOrders.orderStatus='Completed' ;

 accountid   accountid   accountid   RowNo                cnt         skypeUserName contactName friendlyOrderID
----------- ----------- ----------- -------------------- ----------- ------------- ----------- ---------------
1           1           1           1                    2           aaa           aaa         1
2           2           2           2                    2           bbb           bbb         1