gpt4 book ai didi

MySQL :- Adding where clause to this query

转载 作者:行者123 更新时间:2023-11-29 05:11:23 25 4
gpt4 key购买 nike

我有 2 个查询,但我不知道在哪里添加 where 子句。

select supplier_id,count(*) c, party_name, SUM(po_value) po_value
from purchase_order join supplier s on s.id=supplier_id
group by supplier_id
having c>1 ORDER BY c DESC LIMIT 5

第二次查询

select warehouse_id, warehouse_name, SUM(available_quantity) available_quantity
from stock join warehouse s on s.id=warehouse_id
group by warehouse_id

在这两个查询中,最后我必须根据像 where org_id='152'

这样的 id 来获取数据

最佳答案

将其添加到 GROUP BY 之前:

    SELECT supplier_id,count(*) c, party_name, SUM(po_value) po_value
FROM purchase_order A JOIN supplier s ON s.id=supplier_id
GROUP BY supplier_id
WHERE A.org_id='152'
HAVING c>1 ORDER BY c DESC LIMIT 5

    SELECT warehouse_id, warehouse_name, SUM(available_quantity) available_quantity
FROM stock G JOIN warehouse s ON s.id=warehouse_id
WHERE G.org_id='152'
GROUP BY warehouse_id

此外,从可读性的角度来看,我建议使用大写字母来标记标准 SQL 术语:)

本页的第一个示例也很好地回答了您提出的问题:

http://www.w3schools.com/sql/sql_groupby.asp

编辑:更新以消除 where 子句的歧义。

关于MySQL :- Adding where clause to this query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38898359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com