gpt4 book ai didi

mysql - 计算字段中的最小值和最大值

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

我有这样的表1:

number     p     uc_id     date
3 1 g 24/09/2015
4 0 g 24/09/2015
5 1 g 24/09/2015
5 1 f 25/09/2015
3 0 f 25/09/2015
5 1 g 26/09/2015

表 2 如下:

id       name
g Magic
f Blue

我希望 sql 查询返回类似的内容

name      min     max
Magic 1 2
Blue 1 1

我的查询我已经做了类似日期的操作,但我无法检索最小值和最大值

Select 
p.date,
uc.name,
min((Select
uc.name,
count(p.p)
from table1 p, table2 uc
where p.p = 1 and uc.id = p.uc_id
group by uc.name, p.date)) as 'Min',
max((Select uc.name,
count(p.p)
from table1 p, table2 uc
where p.p=1 and uc.id = p.uc_id
group by uc.name, p.date)) as 'Max'
From table1 p, table2 uc
Where uc.id = p.uc_id
group by uc.name

我想对 p=1 的列进行计数,并按日期对它们进行分组,对于每个日期,我想检索之前完成的计数的最小值和最大值。还在另一个表中显示名称。

最佳答案

SQL FIDDLE DEMO

创建一个带有按日期的总计数名称的子查询。

SELECT name, min(nCount) cMin, max(nCount) cMax
FROM (Select
uc.name,
p.date,
count(p) as nCount
From table1 p, table2 uc
Where uc.id = p.uc_id
and p = 1
group by uc.name, p.date) nameCount
GROUP BY name

关于mysql - 计算字段中的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444168/

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