获取与一列的最大值相对应的其他列?

时间:2021-11-17 07:53:04

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.

当它提取数据时,我得到了video_id的正确行,但是它为其他的类别拉出了每个类别的第一行。当我得到第1类的video_id的最大结果时,我得到的是最大ID,但是第一行是url、日期、标题和描述。

How can I have it pull the other columns that correspond with the max ID result?

我怎么能让它拉出与最大ID结果相对应的其他列呢?

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

1 个解决方案

#1


32  

I would try something like this:

我会尝试这样的方法:

SELECT
   s.video_id
   ,s.video_category
   ,s.video_url
   ,s.video_date
   ,s.video_title
   ,short_description
FROM videos s
   JOIN (SELECT MAX(video_id) AS id FROM videos GROUP BY video_category) max
      ON s.video_id = max.id

which is quite faster that your own solution

哪个比你自己的解决方案更快

#1


32  

I would try something like this:

我会尝试这样的方法:

SELECT
   s.video_id
   ,s.video_category
   ,s.video_url
   ,s.video_date
   ,s.video_title
   ,short_description
FROM videos s
   JOIN (SELECT MAX(video_id) AS id FROM videos GROUP BY video_category) max
      ON s.video_id = max.id

which is quite faster that your own solution

哪个比你自己的解决方案更快