Mysql与组_concat和联接表求和
我已经试着解决这个问题好几个星期了,但仍然没有答案 我想显示具有相同产品条形码的库存表的总数量,并显示其项目说明 我有三张桌子 产品表Mysql与组_concat和联接表求和,mysql,sql,group-by,Mysql,Sql,Group By,我已经试着解决这个问题好几个星期了,但仍然没有答案 我想显示具有相同产品条形码的库存表的总数量,并显示其项目说明 我有三张桌子 产品表 ID| barcode | brand | unit | price 1 | 1111111 | Neozep | Tablet | 5.50 2 | 2222222 | Biogesic | Syrup | 7.50 库存表 ID| batch | Total| barcode 1 | 5555555 | 100 | 1111111 2
ID| barcode | brand | unit | price
1 | 1111111 | Neozep | Tablet | 5.50
2 | 2222222 | Biogesic | Syrup | 7.50
库存表
ID| batch | Total| barcode
1 | 5555555 | 100 | 1111111
2 | 6666666 | 500 | 1111111
PRODUCTU表
ID| Name | Amount | Type | barcode
1 | Paracetamol | 250 | mg | 1111111
2 | Amoxicilin | 20 | ml | 1111111
输出应该是这样的
Barcode | Item Description | Price | Total Qty | Amount
1111111 | Paracetamol 250 mg | Amoxicilin 20 ml | P5.50 | 600 | P3300
我目前的Sql语句,但这显然是错误的,希望你们能帮助我
提前谢谢
SELECT
GROUP_CONCAT(DISTINCT productcontains_table.name ,' ',
productcontains_table.Amount,' ',
productcontains_table.Type
ORDER BY productcontains_table.IDno SEPARATOR ' | ' ) AS ItemDescription,
product_table.product_price AS Price,
SUM(inventory_table.inventory_total) AS TotalQuantity
product_table.price AS Price,
SUM(inventory_table.total) AS TotalQuantity,
product_table.price * SUM(inventory_table.total) AS TotalAmount
FROM inventory_table
JOIN product_table
ON product_table.barcode = inventory_table.barcode
JOIN productcontains_table
ON productcontains_table.barcode = product_table.barcode
GROUP BY inventory_table.barcode
修正了几个打字错误:
SELECT inventory_table.barcode,
GROUP_CONCAT(DISTINCT productcontains_table.name,' ', productcontains_table.Amount,' ', productcontains_table.Type SEPARATOR ' | ') AS ItemDescription,
product_table.price AS Price,
SUM(inventory_table.total)/ b.cnt AS TotalQuantity,
product_table.price * SUM(inventory_table.total) / b.cnt AS Amount
FROM inventory_table
JOIN product_table ON product_table.barcode = inventory_table.barcode
JOIN productcontains_table ON productcontains_table.barcode = product_table.barcode
JOIN
( SELECT barcode,
count(productcontains_table.name) AS cnt
FROM productcontains_table )b ON b.barcode=product_table.barcode
GROUP BY inventory_table.barcode,
product_table.price,
inventory_table.barcode
这里缺少一个逗号`和(库存表.库存总量)作为总数量,`是的,我只是忘记了,但那是错误的。感谢这仍然是一个问题,因为您可以看到该组有两个名称,这意味着它还将总金额加两次,因此,总金额不是3300,而是6600,因为该组有两个名称,它还将影响总金额。我认为除以<代码>计数(productcontains\u table.name)应该完成这项工作。我不能让它在小提琴中工作,对不起,我不是想粗鲁地问,但是在字符串中串联计数?这有什么意义?