简体   繁体   中英

SQL joining three tables and split into columns

I have three tables, mess_stock , mess_voucher , add_grocery .

Mess_stock table is below,

+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
|  49 |          5 | 4               |             100 |     10 |     100 |      10 |      10.00 |        1 |
|  50 |         17 | 5               |             111 |     10 |      20 |     101 |      60.00 |        1 |
|  51 |          7 | 3               |             123 |      2 |       1 |     124 |     300.00 |        1 |
|  52 |          7 | 1               |             123 |     20 |      20 |     123 |     500.00 |        2 |
|  53 |         14 | 8               |             100 |      5 |      95 |      10 |      60.00 |        2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+

Mess_voucher table is below

+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
|          5 | VG1001       | 2015-02-19   |
|          6 | VG1001       | 2015-02-20   |
|          7 | VG1002       | 2015-02-20   |
|          8 | VG1002       | 2015-02-19   |
|          9 | MS1001       | 2015-02-20   |
|         10 | VG10012      | 2015-02-19   |
|         11 | 0            | 2015-02-23   |
|         12 | 1            | 2015-02-24   |
|         13 | MS1001       | 2015-02-25   |
|         14 | MS1001       | 2015-02-28   |
|         15 | VG1003       | 2015-02-28   |
|         16 | MS1001       | 2015-02-19   |
|         17 | MS1001       | 2015-02-21   |
+------------+--------------+--------------+

Add_grocery table is below

+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
|   1 | Rice            |            25.00 |
|   3 | Mango           |           150.00 |
|   4 | Coconut         |            22.00 |
|   5 | Banana          |             6.00 |
|   6 | Raddish         |            12.00 |
|   7 | Apple           |           150.00 |
|   8 | Pumkin          |            12.00 |
+-----+-----------------+------------------+

I want to group the sum of pay_amount of mess_stock table. I have used the below query

SELECT opening_balance AS ope_stock, 
       balance AS clo_stock, 
       SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
       SUM(IF(pay_type=2, pay_amount, 0)) est_pay 
FROM mess_stock;

That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.

Try this it will work :

Use Inner Join :

SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM