对“垃圾箱”中的所有物品进行计数。使用 mysql 查询
信息:
我有一个包含任意数量相册的画廊,每个相册中可以有任意数量的图像。
因此,我有一个名为“gallery_album”的表,其中包含所有专辑。我还有一个名为“gallery_item”的表,其中包含每个专辑的所有项目。
我想要做什么:
我希望能够确定垃圾箱中有多少项目(相册和项目)作为一个值。
如果相册位于垃圾箱中,则所有项目也应位于垃圾箱中。
我已经做出了以下声明,但我仍然需要以某种方式获取计数的总和,以便它以一个值呈现,而不是每个专辑一行。 (我不知道这是否是最好的方法,所以请纠正我!)
(当状态为-2时,它被注册为在垃圾箱中。我不想在当状态为-2时将所有项目标记为-2用 -2) 标记专辑
SELECT a.id,
CASE a.status
WHEN -2
THEN (
SELECT COUNT(*)
FROM gallery_item i
WHERE i.fk_album = a.id
) + 1
ELSE (
SELECT COUNT(*)
FROM gallery_item i
WHERE (i.fk_album = a.id) && (i.status = -2)
)
END as cnt, a.status
FROM gallery_album a
GROUP BY a.id
提前致谢!
Information:
I have a gallery with any number of albums, in every album there can be any numbers of images.
I therefore have a table called "gallery_album" with all the albums in. I also have a table called "gallery_item" which contains all the items for every album.
What I want to do:
I want to be able to determine how many items is in the trash bin (albums and items) as one value.
If an album is in the trash, all the items should be in the trash as well.
Ive made the following statement, but I still need to get at SUM of the count in some kind of way so it is presented in one value, instead of one row per album. (I don't know if this is this is the best approach, so please correct me!)
(When status is -2 it is registered as being in the trash bin. I don't want to mark all items with -2 when marking an album with -2)
SELECT a.id,
CASE a.status
WHEN -2
THEN (
SELECT COUNT(*)
FROM gallery_item i
WHERE i.fk_album = a.id
) + 1
ELSE (
SELECT COUNT(*)
FROM gallery_item i
WHERE (i.fk_album = a.id) && (i.status = -2)
)
END as cnt, a.status
FROM gallery_album a
GROUP BY a.id
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,玩了一下后,我解决了......但我仍然想知道是否有更聪明的方法?
这是我的解决方案:
Okay after playing a bit around, I got it solved... But I'd still want to know if there is an even smarter way?
This is my solution: