Leetcode607.销售员(简单)

题目
给定 3 个表: salesperson, company, orders。
输出所有表 salesperson 中,没有向公司 'RED' 销售任何东西的销售员。

表: salesperson

+----------+------+--------+-----------------+-----------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+-----------+
|   1      | John | 100000 |     6           | 4/1/2006  |
|   2      | Amy  | 120000 |     5           | 5/1/2010  |
|   3      | Mark | 65000  |     12          | 12/25/2008|
|   4      | Pam  | 25000  |     25          | 1/1/2005  |
|   5      | Alex | 50000  |     10          | 2/3/2007  |
+----------+------+--------+-----------------+-----------+

表 salesperson 存储了所有销售员的信息。每个销售员都有一个销售员编号 sales_id 和他的名字 name 。

表: company

+---------+--------+------------+
| com_id  |  name  |    city    |
+---------+--------+------------+
|   1     |  RED   |   Boston   |
|   2     | ORANGE |   New York |
|   3     | YELLOW |   Boston   |
|   4     | GREEN  |   Austin   |
+---------+--------+------------+

表 company 存储了所有公司的信息。每个公司都有一个公司编号 com_id 和它的名字 name 。

表: orders

+----------+------------+---------+----------+--------+
| order_id | order_date | com_id  | sales_id | amount |
+----------+------------+---------+----------+--------+
| 1        |   1/1/2014 |    3    |    4     | 100000 |
| 2        |   2/1/2014 |    4    |    5     | 5000   |
| 3        |   3/1/2014 |    1    |    1     | 50000  |
| 4        |   4/1/2014 |    1    |    4     | 25000  |
+----------+----------+---------+----------+--------+

表 orders 存储了所有的销售数据,包括销售员编号 sales_id 和公司编号 com_id 。

输出

+------+
| name | 
+------+
| Amy  | 
| Mark | 
| Alex |
+------+

根据表 orders 中的订单 '3' 和 '4' ,容易看出只有 'John' 和 'Pam' 两个销售员曾经向公司 'RED' 销售过。

生成数据

CREATE TABLE salesperson (sales_id INT,NAME VARCHAR(25),salary INT,commission_rate INT,hire_date DATE);
 
INSERT INTO salesperson VALUES(1,'John',100000,6,'2006-04-01');
INSERT INTO salesperson VALUES (2,'Amy',120000,5,'2010-05-01');
INSERT INTO salesperson VALUES (3,'Mark',65000,6,'2008-12-25');
INSERT INTO salesperson VALUES (4,'Pam',25000,25,'2005-01-01');
INSERT INTO salesperson VALUES (5,'Alex',50000,10,'2007-02-03');


CREATE TABLE company(com_id INT, NAME VARCHAR(20),city VARCHAR(20));
 
INSERT INTO company VALUES (1,'Red','Boston');
INSERT INTO company VALUES (2,'ORANGE','New York');
INSERT INTO company VALUES (3,'YELLO','Boston');
INSERT INTO company VALUES (4,'GREEN','Austin');

CREATE TABLE orders1 (order_id INT,DATE DATE,com_id INT,sales_id INT,amount INT);
 
INSERT INTO orders1 VALUES (1,'2014-01-01',3,4,100000);
INSERT INTO orders1 VALUES (2,'2014-02-01',4,5,5000);
INSERT INTO orders1 VALUES (3,'2014-03-01',1,1,50000);
INSERT INTO orders1 VALUES (4,'2014-04-01',1,4,25000);

解答
先选出公司 'RED' 的 com_id

select com_id
from company
where name = 'RED'

再查找给 'RED' 的 com_id销售过的 sales_id

select sales_id
from orders
where com_id = (select com_id
                 from company
                 where name = 'RED')

再选出不在上边sales_id 的姓名即可

select name
from salesperson
where sales_id not in (select sales_id
                   from orders
                   where com_id = (select com_id
                                    from company
                                    where name = 'RED'));

别的方法
方法一、集合差法
用全部的销售员的集合减去向RED销售过的销售员。

SELECT sp.`name`
FROM salesperson AS sp
LEFT JOIN (SELECT sales_id
FROM orders1
WHERE com_id = (SELECT com_id
                 FROM company
                 WHERE NAME = 'RED')) tmp
ON sp.`sales_id` = tmp.`sales_id`
WHERE tmp.`sales_id` IS NULL;

方法二、
每个人销售产品给公司。找出每个人业务往来的公司。

select *
FROM 
salesperson AS SP LEFT JOIN Orders AS O
    ON(SP.sales_id = O.sales_id)
left join Company AS C
    ON(C.com_id = O.com_id)

注意:存在某些人没有与任何公司有业务往来。也存在某些公司没有订单。所以用left join保留没有业务的人。

对人分组,统计每个人与red公司相关的订单数。并找出订单数为0的人。

SELECT max(SP.NAME) AS `name`
FROM 
salesperson AS SP LEFT JOIN Orders AS O
    ON(SP.sales_id = O.sales_id)
left join Company AS C
    ON(C.com_id = O.com_id)
GROUP BY SP.sales_id
HAVING SUM(If(C.NAME='RED',1,0)) = 0

你可能感兴趣的:(Leetcode607.销售员(简单))