比较两个表ID并获取一个表

时间:2018-05-08 15:21:58

标签: mysql mysqli

我正在尝试编制一个"无卖家的​​列表",我收到一份不同卖家名单的清单,并将其与卖家列表进行比较"又名account_manager_sellers使用NOT IN子句,但这对我不起作用。

plans

+----+-----------+
| id | seller_id |
+----+-----------+
|  1 |      1001 |
|  2 |      1002 |
|  3 |      1002 |
|  4 |      1001 |
|  5 |      1005 |
+----+-----------+

account_manager_sellers

+------+--------------+
|  id  | persons_name |
+------+--------------+
| 1001 | name_1       |
| 1002 | name_2       |
| 1003 | name_3       |
| 1004 | name_4       |
| 1005 | name_5       |
+------+--------------+

预期结果:

+------+--------------+
|  id  | persons_name |
+------+--------------+
| 1003 | name_3       |
| 1004 | name_4       |
+------+--------------+


SELECT DISTINCT(p.seller_id) FROM plans p 
WHERE p.seller_id NOT IN (
    SELECT a.id FROM account_manager_sellers a
)

此代码段正在运行但未返回任何结果。

1 个答案:

答案 0 :(得分:1)

你是在倒退吗?

<强> SQL DEMO:

SELECT  a.id, persons_name 
FROM account_manager_sellers a 
WHERE a.id NOT IN (
    SELECT DISTINCT p.seller_id FROM plans p
);

<强>输出

|   id | persons_name |
|------|--------------|
| 1003 |       name_3 |
| 1004 |       name_4 |