[英]Select all records but exclude linked records
i have the following mysql tables with some sample data. 我有以下mysql表和一些示例数据。
1) table: ai_shipment 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) table: ai_purchase_item 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) table: shipment_purchase_item 3)表:shipment_purchase_item
+----+-------------+------------------+
| id | shipment_id | purchase_item_id |
+----+-------------+------------------+
| 1 | 1 | 2 |
| 2 | 2 | 3 |
+----+-------------+------------------+
basically i am storing all the purchased items details in ai_purchase_item
, ai_shipment
stores the shipping details, and ai_shipment_purchase_item
stores records of items that is shipped. 基本上我将所有购买的商品详细信息存储在
ai_purchase_item
, ai_shipment
存储运送详细信息, ai_shipment_purchase_item
存储已发货商品的记录。
what i am trying to do is, i want to select all records from ai_purchase_item
that is not shipped. 我想要做的是,我想从
ai_purchase_item
中选择所有未发货的记录。 which means the foreign key should not exist in ai_shipment_purchase_item
. 这意味着外键不应该存在于
ai_shipment_purchase_item
。
with reference to above records the result i am expecting to be fetched is. 参考上述记录,我期望获取的结果是。
+----+---------+----------+-----------+-----------+
| 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 |
+----+---------+----------+-----------+-----------+
i tried something like this (i know the sql query is not proper) 我试过这样的东西(我知道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)
thank you. 谢谢。
There are three common approaches for finding values present in one table but missing in another in MySQL: 有三种常见的方法可以找到一个表中存在但在MySQL中另一个表中缺少的值:
LEFT JOIN
NOT EXISTS
NOT IN
Here's the approach using NOT EXISTS
: 这是使用
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
)
And here's a LEFT JOIN
: 这是一个
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
And here's NOT IN
: 这里
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
)
The following article by Quassnoi compares the performance of each of these approaches: Quassnoi的以下文章比较了每种方法的表现:
The conclusion is: 结论是:
the best way to search for missing values in MySQL is using a LEFT JOIN / IS NULL or NOT IN rather than NOT EXISTS.
在MySQL中搜索缺失值的最佳方法是使用LEFT JOIN / IS NULL或NOT IN而不是NOT EXISTS。
select * from ai_purchase_item where id not in (select distinct(purchase_item_id) from shipment_purchase_item )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.