【问题标题】:Get column name which has the max value in a row sql获取在一行sql中具有最大值的列名
【发布时间】:2013-07-08 18:52:16
【问题描述】:

我的数据库中有一个表,我在其中存储新闻文章的类别,每次用户阅读一篇文章时,它都会增加相关列中的值。像这样:

现在我想执行一个查询,我可以在其中获取每条记录具有 4 个最高值的列名。例如,对于用户 9,它将返回:

我尝试了几件事,搜索了很多,但不知道该怎么做。谁能帮帮我?

【问题讨论】:

  • imgur 在我工作的地方被屏蔽了,你能把你的例子的文字放在问题里吗?
  • @MartinSmith:我正在使用 MySQL。
  • @Kevin:sqlfiddle.com/#!2/ff624。示例返回 userID:9 | highest value:Media | 2nd highest value:Wetenschap | 3rd highest value:Economie | 4th highest value:Sport
  • 这听起来像一张表会更好,每个用户每个类别都有一条记录(即,列将是userid, category, articlecount)。然后很容易为每个用户提取 4 个最大的 articlecount 值,并打印相关的类别。

标签: mysql sql max


【解决方案1】:

应该这样做:

select
  userid,
  max(case when rank=1 then name end) as `highest value`,
  max(case when rank=2 then name end) as `2nd highest value`,
  max(case when rank=3 then name end) as `3rd highest value`,
  max(case when rank=4 then name end) as `4th highest value`
from
(
  select userID, @rownum := @rownum + 1 AS rank, name, amt from (
    select userID, Buitenland as amt, 'Buitenland' as name from newsarticles where userID = 9 union
    select userID, Economie, 'Economie' from newsarticles where userID = 9 union
    select userID, Sport, 'Sport' from newsarticles where userID = 9 union
    select userID, Cultuur, 'Cultuur' from newsarticles where userID = 9 union
    select userID, Wetenschap, 'Wetenschap' from newsarticles where userID = 9 union
    select userID, Media, 'Media' from newsarticles where userID = 9
  ) amounts, (SELECT @rownum := 0) r
  order by amt desc
  limit 4
) top4
group by userid

演示:http://www.sqlfiddle.com/#!2/ff624/11

【讨论】:

  • 我不知道 ":=" 在 SQL 中是什么的简写。这是用于 MySQL 或 Oracle 还是其他什么?
  • fiddler 中的演示无法加载。请修复答案中的演示和语法。
【解决方案2】:

一个非常简单的方法如下所示

select userId, substring_index(four_highest,',',1) as 'highest value', substring_index(substring_index(four_highest,',',2),',',-1) as '2th highest value',  substring_index(substring_index(four_highest,',',3),',',-1) as '3 rd highest value',  substring_index(four_highest,',',-1) as '4th highest value'   from
(
select userid, convert(group_concat(val) using utf8) as four_highest from
(
select userId,Buitenland as val,'Buitenland' as col from test where userid=9 union
select userId,Economie as val,' Economie' as col from test where   userid=9 union
select userId,Sport as val ,'Sport' as col from test where  userid=9 union
select userId,Cultuur as val,'Cultuur' as col from test where userid=9 union
select userId,Wetenschap as val,'Wetenschap' as col from test where userid=9 union
select userId,Media as val,'Media' as col from test where  userid=9 order by val desc limit 4
) inner_query
)outer_query;

【讨论】:

    【解决方案3】:

    PL/SQL,也许吧?设置 user_id,查询您的表,将返回的行存储在 nx2 列名和值数组中(其中 n 是列数)并根据值对数组进行排序。

    当然,正确的做法是按照@octern 建议的方式重新设计您的数据库。

    【讨论】:

      【解决方案4】:

      这将使您开始了解从单行的多列中获取最高值的概念(针对您的特定表进行修改 - 我创建了一个假表)。

      create table fake 
      (
        id int Primary Key,
        col1 int,
        col2 int,
        col3 int,
        col4 int 
      )
      insert into fake values (1, 5, 9, 27, 10)
      insert into fake values (2, 3, 5, 1, 20)
      insert into fake values (3, 89, 9, 27, 6)
      insert into fake values (4, 17, 40, 1, 20)
      
      SELECT *,(SELECT Max(v) 
      FROM (VALUES (col1), (col2), (col3), (col4) ) AS value(v))
      FROM fake
      

      【讨论】:

      • 这会获取多列的最大值但不回答问题,具有最高(最大值)值的列的列名是什么。
      • 我无法让这段代码在 MySQL 中工作。正确的做法是使用 GREATEST,如下所示: SELECT *, GREATEST(col1, col2, col3, col4) AS v FROM fake;如前所述,它只获取最高值,而不是具有最高值的列的名称。
      猜你喜欢
      • 2018-02-09
      • 1970-01-01
      • 2013-06-21
      • 2019-07-13
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      相关资源
      最近更新 更多