MySQL-如果table1.id = table2.id> 1

时间:2019-06-25 18:59:28

标签: mysql

好吧,我有3张桌子。 1.产品 2.产品条形码+数量 3.产品类别

我要显示的是具有数量和类别总数的单一产品。

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

此代码显示:

------------------------------------------------------------------
|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      |
------------------------------------------------------------------

我想要显示的是Web中库存和商店中库存的总数,ID,名称,LastUpDate和类别名称。 如果该产品有一个以上的数量记录,则只需显示一行并计算Web中的库存和Store中的库存总数即可。

2 个答案:

答案 0 :(得分:0)

在查询中添加“分组依据”

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

将显示此结果enter image description here

答案 1 :(得分:0)

您可以大大简化查询。不需要嵌套在SELECT中的子查询。另请注意,您应该将其与数字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