创建一个查询,从mysql中选择数据,其中min(i_status)=4

sulc1iza  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(258)

我有两个sql表ordertable

+----------+--------+
| o_number | o_name |
+----------+--------+
|        1 | toys   |
|        2 | bags   |
|        3 | Laptop |
+----------+--------+

和itemtable

+----+----------+--------+-------+--------+----------+
| id | o_number | i_name | i_pcs | i_cost | i_status |
+----+----------+--------+-------+--------+----------+
|  1 |        1 | item1  |     1 |      2 |        1 |
|  2 |        1 | item2  |     1 |      3 |        1 |
|  3 |        2 | item3  |     1 |      4 |        1 |
|  4 |        2 | item4  |     1 |      5 |        2 |
|  5 |        3 | item5  |     1 |      6 |        4 |
|  6 |        3 | item6  |     1 |      7 |        6 |
+----+----------+--------+-------+--------+----------+

我收到所有订单,查询1-结果表1-一切正常

query1
     SELECT OrderTable *, sum(i_cost*i_pcs), min(i_status)
    LEFT JOIN ItemTable ON ItemTable.o_number = OrderTable.o_number
    GROUP BY OrderTable.o_number ORDER BY OrderTable.o_number DESC

结果表1

+----------+--------+------------+------------+
| o_number | o_name | total_cost | min_status |
+----------+--------+------------+------------+
|        1 | toys   |          5 |          1 |
|        2 | bags   |          9 |          1 |
|        3 | Laptop |         13 |          4 |
+----------+--------+------------+------------+

但是我需要接收所有订单,商品的最低状态为4,以及来自此订单的所有商品的总和,不仅id=4的商品我的查询应该是这样的,但是它不会给出结果。

query2
 SELECT OrderTable *, sum(i_cost*i_pcs), min(i_status)
LEFT JOIN ItemTable ON ItemTable.o_number = OrderTable.o_number
WHERE min(i_status) = 4
GROUP BY OrderTable.o_number ORDER BY OrderTable.o_number DESC

我想看到下一个结果:

+----------+--------+------------+------------+
| o_number | o_name | total_cost | min_status |
+----------+--------+------------+------------+
|        3 | Laptop |         13 |          4 |
+----------+--------+------------+------------+

如果我在没有min的情况下将request where更改为where i_status=4,我将得到几乎我所需要的,但是订单数量将更少,因为不是所有的货物都被计算在内。

+----------+--------+------------+------------+
| o_number | o_name | total_cost | min_status |
+----------+--------+------------+------------+
|        3 | Laptop |          6 |          4 |
+----------+--------+------------+------------+
zd287kbt

zd287kbt1#

编辑-感谢您的意见,澄清您的要求,请参阅我的修改后的答案如下。
你问题的第一部分;
“我需要收到所有订单,货物的最低状态为4”
答案是改变 WHERE min(i_status) = 4WHERE i_status >= 4 这将给你的结果 i_status 是“等于或大于”,而不仅仅是“等于”。
如果我理解正确的话,你问题的第二部分;
此订单中所有货物的总和,而不仅仅是id=4的货物
你要的是订单上所有货物的总价。只要这些货物中至少有一件有 i_status 四个或更多。
以下内容应按您的要求执行。

SELECT
    OrderTable.o_number,
    sum(i_cost*i_pcs),
//The following will show you the lowest item status within this order
    min(i_status)
FROM OrderTable

//Use INNER JOIN to only include orders that have items in them
INNER JOIN ItemTable
    ON ItemTable.o_number = OrderTable.o_number
WHERE 
//Use this to find orders which have goods over the minimum status
ItemTable.i_status >= 4
//Then use this to exclude orders which also include an item which is below the minimum status.
    OrderTable.o_number NOT IN
        (
            SELECT o_number
            FROM OrderTable z
            INNER JOIN ItemTable x
              ON z.o_number = x.o_number
              AND x.i_status < 4
        )
GROUP BY OrderTable.o_number
ORDER BY OrderTable.o_number DESC

只需添加一些列标题,并有一个很好的测试。
祝你的项目好运!

js4nwp54

js4nwp542#

谢谢你花时间解决我的问题。但是我需要同时满足两个条件,其中min(i\u status)=4和sum(i\u cost*i\u pcs),其中所有项目状态可能唯一的答案是数组处理,而没有where

相关问题