SQL查询获取与另一列的最大值相对应的列值?
好的,这是我的查询:
SELECT
video_category,
video_url,
video_date,
video_title,
short_description,
MAX(video_id)
FROM
videos
GROUP BY
video_category
当它提取数据时,我得到了 video_id 的正确行,但它为其他类别提取了每个类别的第一行。因此,当我获得类别 1 的 video_id 的最大结果时,我获得最大 ID,但表中的第一行包含 url、日期、标题和描述。
如何让它提取与最大 ID 结果相对应的其他列?
编辑:已修复。
SELECT
*
FROM
videos
WHERE
video_id IN
(
SELECT
DISTINCT
MAX(video_id)
FROM
videos
GROUP BY
video_category
)
ORDER BY
video_category ASC
Ok, this is my query:
SELECT
video_category,
video_url,
video_date,
video_title,
short_description,
MAX(video_id)
FROM
videos
GROUP BY
video_category
When it pulls the data, I get the correct row for the video_id, but it pulls the first row for each category for the others. So when I get the max result for the video_id of category 1, I get the max ID, but the first row in the table for the url, date, title, and description.
How can I have it pull the other columns that correspond with the max ID result?
Edit: Fixed.
SELECT
*
FROM
videos
WHERE
video_id IN
(
SELECT
DISTINCT
MAX(video_id)
FROM
videos
GROUP BY
video_category
)
ORDER BY
video_category ASC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会尝试这样的事情:
这比你自己的解决方案快得多
I would try something like this:
which is quite a lot faster than your own solution
我最近发明了一种新技术来处理 MySQL 中的此类问题。
标量聚合缩减
标量聚合缩减是迄今为止实现此目的的最高性能方法和最简单的方法(就数据库引擎而言),因为它不需要联接、子查询和 CTE。
对于您的查询,它看起来像这样:
标量函数和聚合函数的组合执行以下操作:
如果您想要检索 CHAR 以外类型的值,则可能需要对输出执行额外的 CAST,例如,如果您希望
video_date
为 DATETIME:CAST(SUBSTRING(MAX( CONCAT(LPAD(video_id, 11, '0'), video_date)), 12) AS DATETIME)
与自连接方法相比,此方法的另一个好处是您可以组合其他聚合数据(不仅仅是最新值),或者甚至在同一查询中组合第一个和最后一个项目,例如,
有关解释此方法与其他旧方法相比的优点的更多详细信息,我的完整博客文章位于:https://www.stevenmoseley.com/blog/tech/high-performance-sql-corlated-scalar-aggregate-reduction-queries
I recently invented a new technique to handle this type of problem in MySQL.
SCALAR-AGGREGATE REDUCTION
Scalar-Aggregate Reduction is by far the highest-performance approach and simplest method (in DB engine terms) for accomplishing this, because it requires no joins, no subqueries, and no CTE.
For your query, it would look something like this:
The combination of scalar and aggregate functions does the following:
If you want to retrieve values in types other than CHAR, you may need to performa an additional CAST on the output, e.g. if you want
video_date
to be a DATETIME:CAST(SUBSTRING(MAX(CONCAT(LPAD(video_id, 11, '0'), video_date)), 12) AS DATETIME)
Another benefit of this method over the self-joining method is that you can combine other aggregate data (not just latest values), or even combine first AND last item in the same query, e.g.
For further details explaining the benefits of this method vs other older methods, my full blog post is here: https://www.stevenmoseley.com/blog/tech/high-performance-sql-correlated-scalar-aggregate-reduction-queries
一个稍微“质朴”的解决方案,但应该完成相同的工作:
换句话说,只需生成一个包含所需所有列的表,对其进行排序,使最大值位于顶部,然后将其截断所以你只返回一行。
A slightly more "rustic" solution, but should do the job just the same:
In other words, just produce a table with all of the columns that you want, sort it so that your maximum value is at the top, and chop it off so you only return one row.
这是一个更通用的解决方案(处理重复项)
Here is a more general solution (handles duplicates)
选择视频类别、视频网址、视频日期、视频标题、简短描述、视频 ID
来自视频 t1
其中 video_id in (SELECT max(video_id) FROM 视频 t2 WHERE t1.video_category=t2.video_category );
请提供您的输入和输出记录,以便能够正确理解和测试。
SELECT video_category,video_url,video_date,video_title,short_description,video_id
FROM videos t1
where video_id in (SELECT max(video_id) FROM videos t2 WHERE t1.video_category=t2.video_category );
Please provide your input and output records so that it can be understood properly and tested.