[英]Mysql - Show one row if table1.id = table2.id > 1
Well, I have 3 tables. 好吧,我有3张桌子。 1. Products 2. Product Barcodes + Quantity 3. Product Categories 1.产品2.产品条形码+数量3.产品类别
What I want to display is Single product with the total amount of quantity and category. 我要显示的是具有数量和类别总数的单一产品。
dfs_product_id
-------------------
|ID|Name|Ldate|Cat|
-------------------
|1 |AA |2 |2 |
|2 |BB |3 |3 |
|3 |CC |1 |1 |
-------------------
dfs_product_quantity
------------------------
|ID|PID|Pqw|Pqs|Barcode|
------------------------
|1 |1 |10 |5 |123456 |
|2 |2 |10 |5 |654321 |
|3 |3 |10 |5 |789456 |
|4 |2 |8 |2 |654987 |
|5 |3 |15 |14 |741852 |
|6 |1 |11 |14 |258147 |
------------------------
dfs_product_category
---------
|ID|Name|
---------
|1 |GH |
|2 |TD |
|3 |KL |
---------
SELECT
dfs_product_id`.`ID` AS 'Product ID',
dfs_product_id`.`Name`,
dfs_product_id`.`Ldate` AS 'Last UpDate',
(
SELECT
SUM(`dfs_product_quantity`.`Pqw`)
FROM
`dfs_product_quantity`
WHERE
`dfs_product_quantity`.`PID` = `dfs_product_id`.`ID`
) AS 'Stock In Web',
(
SELECT
SUM(`dfs_product_quantity`.`Pqs`)
FROM
`dfs_product_quantity`
WHERE
`dfs_product_quantity`.`PID` = `dfs_product_id`.`ID`
) AS 'Stock In Store',
`dfs_product_category`.`Name` AS 'Category'
FROM
`dfs_product_id`,
`dfs_product_quantity`,
`dfs_product_category`
WHERE
`dfs_product_id`.`ID` = `dfs_product_quantity`.`PID`
AND
`dfs_product_id`.`Cat` = `dfs_product_category`.`ID`
AND
( 'Stock In Web' + 'Stock In Store' ) < '50'
ORDER BY
`dfs_product_id`.`Ldate` DESC
LIMIT 0 , 50
This code Displays : 此代码显示:
------------------------------------------------------------------
|Product ID|Name|Last UpDate|Stock in Web|Stock in Store|Category|
------------------------------------------------------------------
|2 |BB |3 |18 |7 |KL |
|2 |BB |3 |18 |7 |KL |
|1 |AA |2 |21 |19 |DT |
|1 |AA |2 |21 |19 |DT |
|3 |CC |1 |25 |19 |GH |
|3 |CC |1 |25 |19 |GH |
------------------------------------------------------------------
What, I want is to show the total amount on Stock in web and Stock in store, ID, Name, LastUpDate, and category name. 我要显示的是Web上的库存和商店中的库存的总数,ID,名称,LastUpDate和类别名称。 And if have more then one quantity record of the product, just show one row and count total in Stock in Web and Stock in Store. 并且如果该产品有一个以上的数量记录,则只需显示一行并计算Web中的库存和Store中的库存总数即可。
Add "Group By" in your query 在查询中添加“分组依据”
SELECT
DISTINCT dfs_product_id.ID AS 'Product ID',
dfs_product_id.Name,
dfs_product_id.Ldate AS 'Last UpDate',
(
SELECT
SUM(dfs_product_quantity.Pqw)
FROM
dfs_product_quantity
WHERE
dfs_product_quantity.PID = dfs_product_id.ID
) AS 'Stock In Web',
(
SELECT
SUM(dfs_product_quantity.Pqs)
FROM
dfs_product_quantity
WHERE
dfs_product_quantity.PID = dfs_product_id.ID
) AS 'Stock In Store',
dfs_product_category.Name AS 'Category' FROM
dfs_product_id,
dfs_product_quantity,
dfs_product_category WHERE
dfs_product_id.ID = dfs_product_quantity.PID AND
dfs_product_id.Cat = dfs_product_category.ID AND ( 'Stock In Web' + 'Stock In Store' ) < '50'
GROUP by Category ORDER BY dfs_product_id.Ldate DESC LIMIT 0 , 50
You can simplify the query quite a bit. 您可以简化查询很多。 No need for subqueries nested in SELECT. 不需要嵌套在SELECT中的子查询。 Note also that you shoud compare to numeric 50 not string '50'. 另请注意,您应该将其与数字50而不是字符串'50'进行比较。
SELECT
pid.ID AS 'Product ID',
pid.Name,
pid.Ldate AS 'Last UpDate',
SUM(pq.Pqw) AS 'Stock In Web',
SUM(pq.Pqs) AS 'Stock In Store',
pc.Name AS 'Category'
FROM dfs_product_id pid
JOIN dfs_product_category pc on pid.Cat = pc.ID
JOIN dfs_product_quantity pq on pq.PID = pid.ID
GROUP BY pid.ID, pid.Name, pid.Ldate, pc.Name
HAVING SUM(pq.Pqw) + SUM(pq.Pqs) < 50
ORDER BY pid.Ldate DESC
LIMIT 0 , 50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.