MySQL:高效返回组不为空的分组字段

发布于 2024-09-03 01:04:12 字数 370 浏览 3 评论 0原文

在一个语句中,我尝试通过连接到另一个表来对一个表的行进行分组。我只想获取分组结果不为空的分组行。

前任。项目和类别

SELECT Category.id
FROM Item, Category
WHERE Category.id = Item.categoryId
GROUP BY Category.id
HAVING COUNT(Item.id) > 0

上面的查询给出了我想要的结果,但这很慢,因为它必须计算按 Category.id 分组的所有行。

有什么更有效的方法呢?

我试图执行 Group By LIMIT 以仅检索每组一行。但我的尝试却惨遭失败。知道我该怎么做吗?

谢谢

In one statement I'm trying to group rows of one table by joining to another table. I want to only get grouped rows where their grouped result is not empty.

Ex. Items and Categories

SELECT Category.id
FROM Item, Category
WHERE Category.id = Item.categoryId
GROUP BY Category.id
HAVING COUNT(Item.id) > 0

The above query gives me the results that I want but this is slow, since it has to count all the rows grouped by Category.id.

What's a more effecient way?

I was trying to do a Group By LIMIT to only retrieve one row per group. But my attempts failed horribly. Any idea how I can do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

星光不落少年眉 2024-09-10 01:04:12

试试这个:

SELECT  item.categoryid
FROM    Item
JOIN    Category
ON      Category.id = Item.categoryId
GROUP BY
        item.categoryid
HAVING  COUNT(*) > 0

这与您的原始查询类似,但不会执行您想要的操作。

如果您想选择非空类别,请执行以下操作:

SELECT  category.id
FROM    category
WHERE   id IN
        (
        SELECT  category_id
        FROM    item
        )

为了快速执行此操作,请在 item (category_id) 上创建索引。

Try this:

SELECT  item.categoryid
FROM    Item
JOIN    Category
ON      Category.id = Item.categoryId
GROUP BY
        item.categoryid
HAVING  COUNT(*) > 0

This is similar to your original query, but won't do what you want.

If you want to select non-empty categories, do this:

SELECT  category.id
FROM    category
WHERE   id IN
        (
        SELECT  category_id
        FROM    item
        )

For this to work fast, create an index on item (category_id).

太阳男子 2024-09-10 01:04:12

如果不需要的话,删除类别表怎么样?

SELECT Item.categoryId 
FROM Item
GROUP BY Item.categoryId

我不确定您是否需要 HAVING 子句,因为如果类别中没有项目,则不会创建组。

What about eliminating the Category table if you don't need it?

SELECT Item.categoryId 
FROM Item
GROUP BY Item.categoryId

I'm not sure you even need the HAVING clause since if there is no item in a category it won't create a group.

柒七 2024-09-10 01:04:12

我认为这在功能上是等效的(返回至少有一个项目的每个类别),并且应该更快。

SELECT 
  c.id
FROM 
  Category c
WHERE
  EXISTS (
    select 1 from Item i where i.categoryid = c.categoryID
  )

I think this is functionally equivalent (returns every category that has at least one item), and should be much faster.

SELECT 
  c.id
FROM 
  Category c
WHERE
  EXISTS (
    select 1 from Item i where i.categoryid = c.categoryID
  )
那请放手 2024-09-10 01:04:12

我认为,这只是我的观点,正确的方法是计算所有的东西。也许问题出在另一个地方。

这就是我用来计数的方法,即使有大量数据,它的工作速度也相当快。

SELECT categoryid, COUNT(*) FROM Item GROUP By categoryid

它将为您提供按类别列出的所有项目的哈希值。但它不会包含空类别。

然后,为了检索类别信息,请执行以下操作:

SELECT category.* FROM category
INNER JOIN (SELECT categoryid, COUNT(*) AS n FROM Item GROUP By categoryid) AS item
ON category.id = item.categoryid

I think, and this is just my opinion, that the correct approach IS counting all the stuff. Maybe the problem is in another place.

This is what I use for counting and it works pretty fast, even with a lot of data.

SELECT categoryid, COUNT(*) FROM Item GROUP By categoryid

It will give you a hash with all the items by category. But it will NOT include empty categories.

Then, for retrieveng category information do like this:

SELECT category.* FROM category
INNER JOIN (SELECT categoryid, COUNT(*) AS n FROM Item GROUP By categoryid) AS item
ON category.id = item.categoryid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文