选择所有记录但排除链接记录

时间:2012-07-30 10:36:59

标签: mysql database


我有以下mysql表和一些示例数据。

1)表:ai_shipment

+----+---------------------+---------------------+------------------+
| id | booking_date        | loading_date        | container_number |
+----+---------------------+---------------------+------------------+
|  1 | 2012-08-03 00:00:00 | 2012-08-04 00:00:00 | ABB987987BBC6    |
|  2 | 2012-08-05 00:00:00 | 2012-08-07 00:00:00 | BHJKKU78786GH    |
+----+---------------------+---------------------+------------------+

2)表:ai_purchase_item

+----+---------+----------+-----------+-----------+
| id | item_id | quantity | cost      | rate      |
+----+---------+----------+-----------+-----------+
|  1 |       1 |       50 | 1200.0000 | 1355.0000 |
|  2 |       2 |       20 |  550.0000 |  675.0000 |
|  3 |       4 |       70 |   70.0000 |   70.0000 |
|  4 |       6 |       90 |   90.0000 |   90.0000 |
|  5 |       7 |       80 |   80.0000 |   80.0000 |
+----+---------+----------+-----------+-----------+

3)表:shipment_purchase_item

+----+-------------+------------------+
| id | shipment_id | purchase_item_id |
+----+-------------+------------------+
|  1 |           1 |                2 |
|  2 |           2 |                3 |
+----+-------------+------------------+

基本上我将所有购买的商品详细信息存储在ai_purchase_itemai_shipment存储运费详情,ai_shipment_purchase_item存储商品的记录。

我想要做的是,我想从ai_purchase_item中选择未发送的所有记录。这意味着ai_shipment_purchase_item中不应存在外键。

参考上述记录,我期望获取的结果是。

+----+---------+----------+-----------+-----------+
| id | item_id | quantity | cost      | rate      |
+----+---------+----------+-----------+-----------+
|  1 |       1 |       50 | 1200.0000 | 1355.0000 |
|  4 |       6 |       90 |   90.0000 |   90.0000 |
|  5 |       7 |       80 |   80.0000 |   80.0000 |
+----+---------+----------+-----------+-----------+

我试过这样的事情(我知道sql查询不合适)

 SELECT 
    pi.id, 
    pi.item_id, 
    pi.quantity, 
    pi.cost,
    pi.rate 
 FROM 
    ai_purchase_item pi 
 JOIN 
    ai_shipment_purchase_item spi ON spi.purchase_item_id = pi.id
 WHERE NOT EXISTS (SELECT spi.purchase_item_id WHERE spi.purchase_item_id = pi.id)
谢谢。

2 个答案:

答案 0 :(得分:3)

有三种常见的方法可以找到一个表中存在但在MySQL中另一个表中缺少的值:

  • LEFT JOIN
  • NOT EXISTS
  • NOT IN

以下是使用NOT EXISTS的方法:

SELECT 
    pi.id, 
    pi.item_id, 
    pi.quantity, 
    pi.cost,
    pi.rate 
FROM 
    ai_purchase_item AS pi 
WHERE NOT EXISTS
(
    SELECT *
    FROM ai_shipment_purchase_item AS spi
    WHERE spi.purchase_item_id = pi.id
)

这是一个LEFT JOIN

 SELECT 
    pi.id, 
    pi.item_id, 
    pi.quantity, 
    pi.cost,
    pi.rate 
 FROM 
    ai_purchase_item AS pi 
 LEFT JOIN 
    ai_shipment_purchase_item AS spi ON spi.purchase_item_id = pi.id
 WHERE spi.purchase_item_id IS NULL

这里是NOT IN

SELECT 
    pi.id, 
    pi.item_id, 
    pi.quantity, 
    pi.cost,
    pi.rate 
FROM 
    ai_purchase_item AS pi 
WHERE pi.id NOT IN
(
     SELECT purchase_item_id
     FROM ai_shipment_purchase_item
)

Quassnoi的以下文章比较了每种方法的表现:

结论是:

  

在MySQL中搜索缺失值的最佳方法是使用LEFT JOIN / IS NULL或NOT IN而不是NOT EXISTS。

答案 1 :(得分:1)

select * from ai_purchase_item where id not in (select distinct(purchase_item_id) from shipment_purchase_item )