gpt4 book ai didi

mysql - 使用 WHERE AND GROUP BY 获取最低值的整行

转载 作者:行者123 更新时间:2023-11-30 00:39:13 24 4
gpt4 key购买 nike

我想显示一个预览页面,每个图片组有一张图片,位置最低,Online=Y添加。我想在线获取该团体的全部照片(如果可能)

我有下表的图片库:

PicID(autoinc) | PicGrp(INT) | Online(Y|N) | Pos(0-10,unique within Grp) | ... | Description

1 | 1 | Y | 0
2 | 1 | Y | 1
3 | 2 | N | 1
4 | 2 | N | 3
5 | 2 | Y | 7
6 | 2 | Y | 2
7 | 2 | Y | 10

所以我想要:

1 | 1 | Y | 0 | ... | Description | Total(2)
6 | 2 | Y | 2 | ... | Description | Total(3)

单向..当然不起作用:-)

SELECT PictureID, COUNT(*) AS Total 
FROM Pictures
WHERE MIN(Position) AND Online = 'Y'
GROUP BY PictureGroup

最佳答案

很确定这就是您想要的:

select y.picid, y.picgrp, y.online, y.pos, count(z.picid) as total
from pictures y
join pictures z
on y.picgrp = z.picgrp
and y.online = z.online
where y.pos = (select min(x.pos)
from pictures x
where x.picgrp = y.picgrp
and x.online = 'Y')
and y.online = 'Y'
group by y.picid, y.picgrp, y.online, y.pos

关于mysql - 使用 WHERE AND GROUP BY 获取最低值的整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923004/

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