如何编写这个 MySQL 查询?

发布于 2024-11-15 07:28:39 字数 680 浏览 3 评论 0原文

假设我有一个表,其中包含三个整数字段和一个字符串字段(仅数据)。

例如,它看起来像:

Id      Category    Value   Data
1       1           3       ...
2       1           4       ...
3       2           2       ...
4       2           4       ...
5       3           5       ...
6       3           6       ...
7       3           2       ...
8       4           1       ...

我想要做的是对于每个 Category,在结果集中包含具有最小 Value 字段的行。在这种情况下,它将返回:

Id      Category    Value   Data
1       1           3       ...
3       2           2       ...
7       3           2       ...
8       4           1       ...

如何编写这样的查询?

谢谢!

Suppose I have a table that has three integer fields and one string field (just data).

For example it looks like:

Id      Category    Value   Data
1       1           3       ...
2       1           4       ...
3       2           2       ...
4       2           4       ...
5       3           5       ...
6       3           6       ...
7       3           2       ...
8       4           1       ...

What I want to do is for each Category, include the row with the minimum Value field in the result set. In this case, it would return:

Id      Category    Value   Data
1       1           3       ...
3       2           2       ...
7       3           2       ...
8       4           1       ...

How to write such a query?

Thanks!

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

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

发布评论

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

评论(3

假情假意假温柔 2024-11-22 07:28:39
select T1.Id,
       T1.Category,
       T1.Value,
       T1.Data
from YourTable as T1
  inner join (
               select Category,
                      min(Value) as Value
               from YourTable
               group by Category
             ) as T2
    on T1.Category = T2.Category and
       T1.Value = T2.Value
select T1.Id,
       T1.Category,
       T1.Value,
       T1.Data
from YourTable as T1
  inner join (
               select Category,
                      min(Value) as Value
               from YourTable
               group by Category
             ) as T2
    on T1.Category = T2.Category and
       T1.Value = T2.Value
迷雾森÷林ヴ 2024-11-22 07:28:39
select *
from mytable t
join (select category, min(value) as value from mytable group by 1) x
    on t.value = x.value and t.category = x.category

内部选择获取最终结果所需的类别和值,然后连接回主表以获取所有列。

select *
from mytable t
join (select category, min(value) as value from mytable group by 1) x
    on t.value = x.value and t.category = x.category

The inner select gets the categories and values needed for the final result, then you join back to the main table to get all the columns.

逐鹿 2024-11-22 07:28:39
SELECT category, MIN(value) as Value
FROM table
GROUP BY category

使用 MIN 函数和 GROUP BY 类别列上的结果。

SELECT category, MIN(value) as Value
FROM table
GROUP BY category

Use the MIN function and GROUP BY the results on the Category column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文