2012-05-24 265 views
3

我在我的數據庫中有一個表,用於存儲newsarticles的類別,每次用戶讀取文章時,都會增加關聯列中的值。就像這樣:獲取在一行中具有最大值的列名sql

enter image description here

現在我想執行一個查詢在那裏我可以得到每條記錄的4個最高值的列名。例如,對於用戶9,它會返回此:

enter image description here

我已經試過幾件事情,搜索了很多,但不知道該怎麼做。誰能幫我?

+0

imgur,我的工作受阻,你可以把你的例子文成問題嗎? – Kevin

+0

@MartinSmith:我正在使用MySQL。 – Robin

+1

@Kevin:http://www.sqlfiddle.com/#!2/ff624。示例返回'userID:9 |最高值:Media |第二高值:Wetenschap |第三最高值:Economie |第四最高值:Sport' – mellamokb

回答

3

這應做到:

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

+0

我不知道「:=」是SQL中的簡寫。這是MySQL還是Oracle或其他? – user2378769

+0

在小提琴手演示不加載。請在答案中修復演示和語法。 – user2378769

0

PL/SQL,也許?設置user_id,查詢您的表,將返回的行存儲在列名稱和值(其中n是列數)的nx2數組中,並根據這些值對數組進行排序。

當然,正確的做法是以@octern建議的方式重新設計數據庫。

0

這會讓你開始從單行上的多列中獲取最高值的概念(修改您的特定表格 - 我創建了一個假表格)。

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 
+0

這將獲得多個列的最大值,但不回答問題,具有最高(最大)值的列的列名稱是什麼。 – user2378769

+0

我無法讓這段代碼在MySQL中工作。正確的做法是使用GREATEST,如下所示: SELECT *,GREATEST(col1,col2,col3,col4)AS v FROM fake; 如前所述,它只獲取最高值,而不是具有最高值的列的名稱。 – Ian

1

這樣做的一個非常簡單的方法如下所示

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; 
相關問題