[英]Get full row for lowest value with WHERE AND GROUP BY
我想顯示一個預覽頁面,其中每個圖片組的圖片中位置最低的位置包含1張圖片,並且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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.