gpt4 book ai didi

mysql - 从子查询 SQL 选择最大数据,但它显示子查询的所有结果

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

SELECT MAX( t.valuesum ) AS total, t.codebook
FROM (
SELECT SUM( value ) AS valuesum, codebook
FROM stock
GROUP BY codebook
) t
GROUP BY t.codebook

在子查询中,输出是:

b001 35,
b002 20,
b0003 11

我只想看:b001 35.请帮帮我。

最佳答案

使用 where 条件的第一个解决方案:

SELECT t1.valuesum, t1.codebook
FROM (
SELECT SUM( s.value ) AS valuesum, s.codebook
FROM stock s
GROUP BY s.codebook
) t1
WHERE t1.valuesum in (SELECT MAX( t2.valuesum ) AS total
FROM (
SELECT SUM( s2.value ) AS valuesum, s2.codebook
FROM stock s2
GROUP BY s2.codebook
)t2
)

SQL Fiddle演示

使用 having 条件的第二个解决方案:

SELECT max(t1.valuesum), t1.codebook
FROM (
SELECT SUM( s.value ) AS valuesum, s.codebook
FROM stock s
GROUP BY s.codebook
) t1
GROUP BY t1.codebook
HAVING max(t1.valuesum) = (SELECT MAX( t2.valuesum ) AS total
FROM (
SELECT SUM( s2.value ) AS valuesum, s2.codebook
FROM stock s2
GROUP BY s2.codebook
)t2
)

SQL Fiddle演示

关于mysql - 从子查询 SQL 选择最大数据,但它显示子查询的所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12562389/

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