leetcode607. 销售员(SQL)
給定 3 個(gè)表:?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 存儲(chǔ)了所有銷售員的信息。每個(gè)銷售員都有一個(gè)銷售員編號(hào)?sales_id 和他的名字?name?。
表:?company
+---------+--------+------------+
| com_id ?| ?name ?| ? ?city ? ?|
+---------+--------+------------+
| ? 1 ? ? | ?RED ? | ? Boston ? |
| ? 2 ? ? | ORANGE | ? New York |
| ? 3 ? ? | YELLOW | ? Boston ? |
| ? 4 ? ? | GREEN ?| ? Austin ? |
+---------+--------+------------+
表?company?存儲(chǔ)了所有公司的信息。每個(gè)公司都有一個(gè)公司編號(hào)?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?存儲(chǔ)了所有的銷售數(shù)據(jù),包括銷售員編號(hào) sales_id 和公司編號(hào) com_id?。
輸出
+------+
| name |?
+------+
| Amy ?|?
| Mark |?
| Alex |
+------+
解釋
根據(jù)表?orders?中的訂單 '3' 和 '4' ,容易看出只有 'John' 和 'Pam' 兩個(gè)銷售員曾經(jīng)向公司 'RED' 銷售過。
所以我們需要輸出表?salesperson?中所有其他人的名字。
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'));not in內(nèi)部:把記錄里和RED公司的id有關(guān)的個(gè)人查出來即可。
然后判斷沒關(guān)系的,不在此名單中。
?
總結(jié)
以上是生活随笔為你收集整理的leetcode607. 销售员(SQL)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谢娜刘烨分手原因(谢娜首次公开与刘烨分手
- 下一篇: 偷心情圣(偷心情圣电影在线观看)