[英]How can I get the price to depend on id using MySQL
I multiply the price
and SUM of quantity
to get the total. 我将price
和quantity
的总和相乘得到总数。 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
. 价格与第一行10
的sup_med_id
不匹配。 How can I get the price to depends on sup_med_id
? 如何获得价格取决于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. 让我知道您是否需要帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.