有条件的加入不同的表

问题描述 投票:20回答:6

我想知道用户是否在2个相关表的any中有一个条目。

表格

USER (user_id)
EMPLOYEE (id, user_id)
STUDENT (id, user_id)

用户可能有一名雇员和/或学生的录入。如何在一个查询中获取该信息?我尝试过:

select * from [user] u
inner join employee e 
    on e.user_id = case when e.user_id is not NULL 
                        then u.user_id 
                        else null 
                   end
inner join student s 
    on s.user_id = case when s.user_id is not NULL 
                        then u.user_id 
                        else null 
                   end

但是它将仅返回在both表中具有条目的用户。

SQL Fiddle example

sql sql-server-2008 join conditional
6个回答
22
投票

您可以使用外部联接:

select *
  from USER u
  left outer join EMPLOYEE e ON u.user_id = e.user_id
  left outer join STUDENT s ON u.user_id = s.user_id
 where s.user_id is not null or e.user_id is not null

或者(如果您对EMPLOYEE或STUDENT表中的数据不感兴趣)

select *
  from USER u
 where exists (select 1 from EMPLOYEE e where e.user_id = u.user_id)
    or exists (select 1 from STUDENT s  where s.user_id = u.user_id)

8
投票

如果您希望将所有用户数据汇总在一起,则可能有:

SELECT 
    user_id
    ,'Employee' AS Source
FROM 
    employee
UNION 
SELECT 
    user_id
    ,'Student' AS Source
FROM 
    student

http://sqlfiddle.com/#!3/90216/22

也可以通过完全联接和CASE语句来完成:

SELECT 
    ISNULL(e.user_id,s.user_id) AS user_id
    ,CASE WHEN e.user_id IS NULL THEN 'Student' 
        ELSE 'Employee'
    END AS SOURCE
FROM 
    employee AS e 
    FULL JOIN student AS s
        ON s.user_id = e.user_id

http://sqlfiddle.com/#!3/90216/29

后者会将既是学生又是雇员的人合并为一排,称他们为雇员。比较:

http://sqlfiddle.com/#!3/2aa3e/1http://sqlfiddle.com/#!3/2aa3e/2

我使用户1成为学生和雇员的位置


5
投票

这种解决方案也可以为您提供帮助。

SELECT  S.*, P.*

,CASE
    WHEN S.ShipmentType = 'import' THEN SP.SupplierName
    WHEN S.ShipmentType = 'export' THEN C.CustomerName
END AS ShipmentDesination

FROM            tblShippments   S 
INNER JOIN      tblProducts     P   ON S.productId = P.productID  
LEFT OUTER JOIN tblCustomers    C   ON S.companyId = C.customerId AND S.ShipmentType = 'export'
LEFT OUTER JOIN tblSuppliers    SP  ON S.companyId = SP.supplierId AND S.ShipmentType = 'import'

2
投票

如果将employee和student表视为一个表,则可以使用左连接:

select * 
from user u
left join 
(
   select 'Employee' as UserType,
          id,
          user_id
     from employee e 
    union all
   select 'Student',
          id,
          user_id
     from student s 
) r
  ON u.user_id = r.user_id

0
投票

如何将UNION编写为2条单独的SELECT语句

例如: SELECT * FROM User U JOIN Employee E ON E.User_Id = U.User_Id UNION SELECT * FROM User U JOIN student S ON S.User_Id = U.User_Id 我看不到您为什么需要CASE语句,它看起来是多余的。如果要所有用户并显示空值,请使用LEFT OUTER JOIN。


0
投票

早上好,我遇到类似的问题。

我正在尝试让SQL为我争取特定工作的发票日期。重要的日期通常是保险公司的发票,因此我的加入声明为[]

Left Join Public.Invoices  INV ON INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.InsNum 

如果找不到发票,那么它很有可能是现金工作,所以我需要使用此联接

Left Join Public.Invoices  INV  ON INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.PK_Customer

所以我已经尝试过

IF QSM.InsNum <> 0
    Left Join Public.Invoices  INV ON INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.InsNum 
    Else 
 Left Join Public.Invoices  INV  ON QSM.InsNum = 0 AND INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.PK_Customer 
    END

不幸的是我在IF语句上遇到错误。

这是我的完整声明

SELECT DISTINCT ON (QSM.PK_QuoSum) 
    QSM.PK_QuoSum , QSM.PK_Customer , QSM.PK_CarCust , QSM.InsNum , QSM.nDate , 
    QHD2.PBeatHrs , QHD2.FrameHrs , QHD2.SAssHrs , QHD2.MechHrs , QHD2.PaintHrs , 
    QHD2.PBeatMoney , QHD2.FrameMoney , QHD2.SAssMoney , QHD2.MechMoney , QHD2.PaintMoney , 
    QHD2.Total , 
    QHD4.Total , 
    QJB.nClosed , QJB.StartDate ,  
    CUS.FName ,CUS.SName , CUS.Phone1 , CUS.EMail, 
    CUSINS.SName, 
    INV.nDate, 
    CCR.ChasiosNo, CCR.nYear , CCR.RegNo, 
    MAN.PK_CarMan , MAN.cName, 
    CAR.cModel ,  
    EXTRACT (MONTH FROM QJB.DeliverdDate) As MonthNo 
    From Public.QuoSum          QSM 
    Left Join Public.QuoHead   QHD2     ON QHD2.PK_QuoSum = QSM.PK_QuoSum AND QHD2.nStatus = 2  
    Left Join Public.QuoHead   QHD4     ON QHD4.PK_QuoSum = QSM.PK_QuoSum AND QHD4.nStatus = 4 
    Left Join Public.QuoJob      QJB       ON QJB.PK_QuoSum = QSM.PK_QuoSum 
    Left Join Public.Customer   CUS       ON  CUS.PK_Customer = QSM.PK_Customer 
    Left Join Public.CarCust      CCR       ON CCR.PK_CarCust = QSM.PK_CarCust 
    Left Join Public.CarMan      MAN      ON MAN.PK_CarMan = CCR.PK_CarMan 
    Left Join Public.CarMast     CAR       ON CAR.PK_CarMast = CCR.PK_CarMast 
    Left Join Public.Customer   CUSINS  ON CUSINS.PK_Customer = QSM.InsNum 
    Left Join Public.Invoices     INV        ON QSM.InsNum <> 0 AND INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.InsNum 
    Left Join Public.Invoices     INV        ON QSM.InsNum = 0 AND INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.InsNum 
IF QSM.InsNum <> 0
    Left Join Public.Invoices  INV ON INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.InsNum 
    Else 
 Left Join Public.Invoices  INV  ON QSM.InsNum = 0 AND INV.PK_QuoSum = QSM.PK_QuoSum And INV.nDebtor = QSM.PK_Customer 
    END
    Where  
     QJB.DeliverdDate BETWEEN  '01-01-19' And '31-12-19'
     And QJB.nclosed < 2  
     And QJB.nTemp = 0 
© www.soinside.com 2019 - 2024. All rights reserved.