简体   繁体   中英

How can I get the price to depend on id using MySQL

I multiply the price and SUM of quantity to get the total. The problem when I multiply the two-column from the different table I get the wrong result. Price is not matched to the sup_med_id it just took the first row which is 10 . How can I get the price to depends on sup_med_id ?

sup_med_id  price  sup_id      rec_order_dtls  sup_med_id  rec_quantity
1           10.00     2           1               1           200
2           12.00     2           2               2           100

date_id   date
1         2019-01-01
2         2019-01-02
...       ...

Output

Month  Quantity  Total  Supplier
...
Jul   0          0      NULL
Aug   300        3000   Unilever    

I didn't put all the data and tables above. I wish this was the result below.

Expected Output

Month  Quantity  Total  Supplier
...
Jul   0          0      NULL
Aug   300        3200   Unilever 
 SELECT DATE_FORMAT(tbl_date.date, '%b') 
 AS Month, 
  COUNT(tbl_purchase_received_details.purchase_received_id) 
 AS Total_Order, 
  SUM(IFNULL(tbl_purchase_received_details.received_quantity,0)) 
 AS Quantity, 
  IFNULL((tbl_supplier_medicine.price) * 
  SUM(tbl_purchase_received_details.received_quantity),0) 
 AS Total_Amount, 
  tbl_supplier.supplier_name 
 AS Supplier FROM tbl_date 
 LEFT JOIN 
  tbl_purchase_received ON tbl_purchase_received.date_received = tbl_date.date 
 LEFT JOIN 
  tbl_purchase_received_details ON tbl_purchase_received.purchase_received_id 
  = tbl_purchase_received_details.purchase_received_id 
 LEFT JOIN 
  tbl_supplier_medicine ON tbl_supplier_medicine.supplier_medicine_id = 
  tbl_purchase_received_details.supplier_medicine_id 
 LEFT JOIN tbl_supplier ON tbl_supplier.supplier_id = 
  tbl_supplier_medicine.supplier_id 
 WHERE YEAR(tbl_date.date) = YEAR(NOW()) 
 GROUP BY DATE_FORMAT(tbl_date.date, '%b')
 ORDER BY tbl_date.date

Hello If I understand your question properly then you need to multiply quantity and price to get the total. If that is correct then you can use the below query :

select tt.rec_quantity as quantity, (price * rec_quantity) as total from temp t inner join temp2 tt on t.sup_med_id=tt.sup_med_id;

If one of the tables can have no match data then you can use left join . Let me know if you need help.

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