对“垃圾箱”中的所有物品进行计数。使用 mysql 查询

发布于 2024-12-04 13:13:45 字数 738 浏览 1 评论 0原文

信息

我有一个包含任意数量相册的画廊,每个相册中可以有任意数量的图像。

因此,我有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-12-11 13:13:45

好吧,玩了一下后,我解决了......但我仍然想知道是否有更聪明的方法?

这是我的解决方案:

SELECT SUM(albums_and_items.cnt)
FROM (
  SELECT
  CASE a.status
    WHEN -2
    THEN (
      SELECT COUNT(*)
      FROM gallery_item AS i
      WHERE i.fk_album = a.id
    ) + 1
    ELSE (
      SELECT COUNT(*)
      FROM gallery_item AS i
      WHERE (i.fk_album = a.id) && (i.status = -2)
    )
  END as cnt
  FROM gallery_album AS a
  GROUP BY a.id
) AS albums_and_items

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:

SELECT SUM(albums_and_items.cnt)
FROM (
  SELECT
  CASE a.status
    WHEN -2
    THEN (
      SELECT COUNT(*)
      FROM gallery_item AS i
      WHERE i.fk_album = a.id
    ) + 1
    ELSE (
      SELECT COUNT(*)
      FROM gallery_item AS i
      WHERE (i.fk_album = a.id) && (i.status = -2)
    )
  END as cnt
  FROM gallery_album AS a
  GROUP BY a.id
) AS albums_and_items
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文