简体   繁体   English

MySQL-如果table1.id = table2.id> 1则显示一行

[英]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

will show this result 将显示此结果 在此处输入图片说明

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.

相关问题 MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id 在我的MySQL中,使用单词JOIN或使用:table1.id = table2.id有什么区别? - In my MySQL what is the difference between using the word JOIN or using: table1.id=table2.id 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MySQL 如果 table1.id = table2.table1_id 查询应该在第一个表中插入第二个表 - MySQL Query should insert a second table into the first table if table1.id = table2.table1_id Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM