Muhammad Taqi
Muhammad Taqi

Reputation: 5424

Fetch data from multiple tables using views

i have following tables,

mysql> select * from purchase_order;
+-------------------+-------------------------+-------+---------------------+
| purchase_order_id | purchase_order          | cost  | created_on          |
+-------------------+-------------------------+-------+---------------------+
|                 1 | Dell Computer 000001256 | 10000 | 2015-02-19 22:14:52 |
|                 2 | HP Computer 000001256   | 50000 | 2015-02-19 22:14:52 |
+-------------------+-------------------------+-------+---------------------+
2 rows in set (0.00 sec)

mysql> select * from purchase_order_detail;
+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
|                        1 |                 1 |     279 |              100 |
|                        2 |                 1 |     286 |              100 |
|                        3 |                 2 |     279 |              200 |
|                        4 |                 2 |     286 |              300 |
+--------------------------+-------------------+---------+------------------+
4 rows in set (0.00 sec)

mysql> select * from delivery_order;
+-------------------+--------------------------+-------------------+---------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity | recieved_on         |
+-------------------+--------------------------+-------------------+---------------------+
|                 1 |                        1 |                50 | 2015-02-19 22:22:51 |
|                 2 |                        2 |                50 | 2015-02-19 22:24:59 |
|                 3 |                        1 |                50 | 2015-02-19 22:34:14 |
|                 4 |                        3 |                70 | 2015-02-20 11:11:31 |
|                 5 |                        4 |               150 | 2015-02-20 11:11:31 |
|                 6 |                        3 |                90 | 2015-02-20 11:12:20 |
|                 7 |                        4 |               100 | 2015-02-20 11:12:20 |
|                 8 |                        3 |                40 | 2015-02-20 11:12:55 |
|                 9 |                        4 |                50 | 2015-02-20 11:12:55 |
+-------------------+--------------------------+-------------------+---------------------+
9 rows in set (0.00 sec)

mysql> select * from stock;
+----------+-------------------+------------+----------+
| stock_id | delivery_order_id | project_id | quantity |
+----------+-------------------+------------+----------+
|        1 |                 1 |          1 |       30 |
|        2 |                 1 |          2 |       20 |
|        3 |                 2 |          1 |       50 |
|        4 |                 3 |          1 |       40 |
|        5 |                 3 |          2 |       10 |
+----------+-------------------+------------+----------+

i want to fetch all purchase_order and their quantity in stock for those purchase who has item_id = 279 in it.

The Goal is to create views in which i simply pass the item_id and it fetches the list of all purchase_order in which item_id that will be input parameter and their total quantity in stock.

so, far i have write this query, i am new to mysql and views

select po.purchase_order_id, po.purchase_order from purchase_order po, purchase_order_detail pod where po.purchase_order_id = pod.purchase_order_id and pod.item_id = 279;
+-------------------+-------------------------+
| purchase_order_id | purchase_order          |
+-------------------+-------------------------+
|                 1 | Dell Computer 000001256 |
|                 2 | HP Computer 000001256   |
+-------------------+-------------------------+

but it want some thing like this,

+-------------------+-------------------------+----------+-----------+
| purchase_order_id | purchase_order          | item_id  | quantity  |
+-------------------+-------------------------+----------+-----------+
|                 1 | Dell Computer 000001256 | 279      |    100    +
|                 2 | HP Computer 000001256   | 279      |     0     +
+-------------------+-------------------------+----------+-----------+

Upvotes: 0

Views: 52

Answers (1)

Jens
Jens

Reputation: 69440

Try this untested query:

select po.purchase_order_id, po.purchase_order, sum(s.quantity) 
from purchase_order po 
join  purchase_order_detail pod on po.purchase_order_id = pod.purchase_order_id
join  delivery_order do on do.purchase_order_id = pod.purchase_order_id
join  stock s on s.delivery_order_id  = do.delivery_order_id 
where pod.item_id = 279
group by po.purchase_order_id, po.purchase_order;

Upvotes: 1

Related Questions