27

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

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

但它只会返回两个表中都有条目的用户。

SQL 小提琴示例

4

7 回答 7

29

您可以使用外部联接:

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)
于 2012-04-23T10:57:45.597 回答
9

如果您想将所有用户数据放在一起您可能有:

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 成为学生和员工的地方

于 2012-04-23T11:07:11.237 回答
6

这样的解决方案也可以帮助你。

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'
于 2014-04-12T08:54:55.890 回答
3

如果您将员工表和学生表视为一个表,则可以使用左连接:

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
于 2012-04-23T10:51:24.167 回答
0

你写为 2 个单独的 SELECT 语句的 UNION 怎么样

例如: 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。

于 2012-04-23T11:01:54.143 回答
0

适用于 mysql v5.6+
架构:

CREATE TABLE USER (user_id INT);
CREATE TABLE employee (id INT, user_id INT);
CREATE TABLE student (id INT, user_id INT);

INSERT INTO USER SELECT 1;
INSERT INTO USER SELECT 2;
INSERT INTO USER SELECT 3;
INSERT INTO USER SELECT 4;

INSERT INTO employee SELECT 1, 1;
INSERT INTO employee SELECT 2, 4;

INSERT INTO student SELECT 1, 2;

第一个解决方案,完全外连接不适用于 mysql v5.6+:

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

替代解决方案:

SELECT   IF(e.user_id IS NULL,s.user_id,e.user_id) AS user_id,
 CASE WHEN e.user_id IS NULL THEN 'Student' 
            ELSE 'Employee'
        END AS SOURCE
 FROM   USER AS 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

其他替代解决方案:

 SELECT u.user_id,SOURCE 
FROM USER u
INNER JOIN 
(
   SELECT 'Employee' AS SOURCE,id,user_id FROM employee e 
    UNION ALL
   SELECT 'Student',id,user_id  FROM student s 
) r ON u.user_id = r.user_id
于 2021-08-27T17:02:26.560 回答
-1
SELECT   OrderID, Quantity,  O.ProductID , ProductName,
CASE
WHEN Quantity > 3 THEN 'The quantity is More than 3'
WHEN Quantity = 3 THEN 'The quantity is Equal to  3'
ELSE 'The quantity is Less than 3'
END AS QuantityText

FROM            tb_OrderDetail O 
INNER JOIN      tb_Product     P   ON O.ProductID = P.ProductID
于 2020-05-06T12:42:15.863 回答