我有以下表结构,
mysql> select * from purchase_order_detail;
+--------------------------+-------------------+---------+------------------+-------------------------+---------------------+------------+---------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity | created_by | created_on | updated_by | updated_on |
+--------------------------+-------------------+---------+------------------+-------------------------+---------------------+------------+---------------------+
| 1 | 1 | 279 | 100 | NULL | 2015-02-21 01:42:06 | NULL | 2015-02-21 01:42:06 |
| 2 | 1 | 286 | 100 | NULL | 2015-02-21 01:42:06 | NULL | 2015-02-21 01:42:06 |
| 3 | 2 | 279 | 200 | NULL | 2015-02-21 01:42:34 | NULL | 2015-02-21 01:42:34 |
| 4 | 2 | 286 | 300 | NULL | 2015-02-21 01:42:34 | NULL | 2015-02-21 01:42:34 |
| 23 | 1 | 287 | 100 | taqi.official@gmail.com | 2015-02-22 14:37:04 | NULL | 2015-02-22 14:37:04 |
| 24 | 28 | 286 | 43 | admin@gmail.com | 2015-02-22 18:10:15 | NULL | 2015-02-22 18:10:15 |
| 25 | 28 | 287 | 43 | admin@gmail.com | 2015-02-22 18:10:23 | NULL | 2015-02-22 18:10:23 |
+--------------------------+-------------------+---------+------------------+-------------------------+---------------------+------------+---------------------+
7 rows in set (0.00 sec)
mysql> select * from delivery_order;
+-------------------+--------------------------+-------------------+---------------------+-----------------+---------------------+------------+---------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity | recieved_on | created_by | created_on | updated_by | updated_on |
+-------------------+--------------------------+-------------------+---------------------+-----------------+---------------------+------------+---------------------+
| 1 | 1 | 50 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:44:15 | NULL | 2015-02-21 01:44:15 |
| 2 | 2 | 50 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:44:15 | NULL | 2015-02-21 01:44:15 |
| 3 | 1 | 50 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:44:35 | NULL | 2015-02-21 01:44:35 |
| 4 | 3 | 70 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:44:35 | NULL | 2015-02-21 01:44:35 |
| 5 | 4 | 150 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:45:04 | NULL | 2015-02-21 01:45:04 |
| 6 | 3 | 90 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:45:04 | NULL | 2015-02-21 01:45:04 |
| 7 | 4 | 100 | 0000-00-00 00:00:00 | NULL | 2015-02-21 01:45:28 | NULL | 2015-02-21 01:45:28 |
| 28 | 24 | 34 | 2015-02-21 00:00:00 | admin@gmail.com | 2015-02-22 18:10:15 | NULL | 2015-02-22 18:10:15 |
| 29 | 25 | 34 | 2015-02-21 00:00:00 | admin@gmail.com | 2015-02-22 18:10:23 | NULL | 2015-02-22 18:10:23 |
+-------------------+--------------------------+-------------------+---------------------+-----------------+---------------------+------------+---------------------+
9 rows in set (0.00 sec)
mysql> select * from stock;
+----------+-------------------+------------+----------+---------------+------------+---------------------+------------+---------------------+
| stock_id | delivery_order_id | project_id | quantity | stock_type_id | created_by | created_on | updated_by | updated_on |
+----------+-------------------+------------+----------+---------------+------------+---------------------+------------+---------------------+
| 1 | 1 | 1 | 30 | 1 | NULL | 2015-02-21 01:46:38 | NULL | 2015-02-21 01:46:38 |
| 2 | 1 | 2 | 20 | 1 | NULL | 2015-02-21 01:46:38 | NULL | 2015-02-21 01:46:38 |
| 3 | 2 | 1 | 50 | 1 | NULL | 2015-02-21 01:47:30 | NULL | 2015-02-21 01:47:30 |
| 4 | 3 | 1 | 40 | 1 | NULL | 2015-02-21 01:47:30 | NULL | 2015-02-21 01:47:30 |
| 5 | 3 | 2 | 10 | 1 | NULL | 2015-02-21 01:47:51 | NULL | 2015-02-21 01:47:51 |
+----------+-------------------+------------+----------+---------------+------------+---------------------+------------+---------------------+
我希望获得所有stock_id,其中purchase_order_id = 1,item_id = 279。 我写了这个查询
select s.stock_id
from stock s
, delivery_order do
, purchase_order_detail pod
, purchase_order po
where s.delivery_order_id = do.delivery_order_id
and pod.purchase_order_detail_id = do.purchase_order_detail_id
and po.purchase_order_id = 1
and pod.item_id = 279;
但得到以下结果,这是不正确的......
+----------+
| stock_id |
+----------+
| 1 |
| 2 |
| 4 |
| 5 |
+----------+
我希望在buy_order_detail表格中针对特定的purchase_order_id和item_id获取stock_id。
答案 0 :(得分:1)
你可以试试这个:
SELECT DISTINCT
s.stock_id ID
FROM stock s
LEFT JOIN delivery_order do
ON s.delivery_order_id = do.delivery_order_id
LEFT JOIN purchase_order_detail pod
ON do.purchase_order_detail_id = pod.purchase_order_detail_id
WHERE pod.purchase_order_id = 1
AND pod.item_id = 279;