mysql 控制分組個數 MYSQL 按限制分組

2021-10-17 19:44:35 字數 4642 閱讀 6881

有沒有一種簡單的方法可以將group by結果限制在前2位。以下查詢返回所有結果。使用「 limit 2」將整個列表減少到僅前2個條目。

select distinct(rating_name),

id_markets,

sum(rating_good) 'good',

sum(rating_neutral)'neutral',

sum(rating_bad) 'bad'

from ratings

where rating_year=year(curdate()) and rating_week= week(curdate(),1)

group by rating_name,id_markets

order by rating_name, sum(rating_good)

desc

結果如下:

波蘭78 48 24 12

波蘭1 15 5 0

波蘭23 12 6 3

波蘭2 5 0 0

波蘭3 0 5 0

波蘭4 0 0 5

愛爾蘭1 9 3 0

愛爾蘭2 3 0 0

愛爾蘭3 0 3 0

愛爾蘭4 0 0 3

法國12 24 12 6

法國1 3 1 0

法國231 1 0 0

法國2 1 0 0

法國4 0 0 1

法國3 0 1 0

謝謝喬恩

根據要求,我附上了**結構和一些測試資料的副本。我的目標是建立乙個檢視,該檢視具有每個唯一的rating_name的前2個結果

create table `zzratings` (

`id` int(11) not null auto_increment,

`id_markets` int(11) default null,

`id_account` int(11) default null,

`id_users` int(11) default null,

`datetime` timestamp null default current_timestamp,

`rating_good` int(11) default null,

`rating_neutral` int(11) default null,

`rating_bad` int(11) default null,

`rating_name` varchar(32) default null,

`rating_year` smallint(4) default null,

`rating_week` tinyint(4) default null,

`cash_balance` decimal(9,6) default null,

`cash_spend` decimal(9,6) default null,

primary key (`id`),

key `rating_year` (`rating_year`),

key `rating_week` (`rating_week`),

key `rating_name` (`rating_name`)

) engine=myisam auto_increment=2166690 default charset=latin1;

insert into `zzratings` (`id`,`id_markets`,`id_account`,`id_users`,`datetime`,`rating_good`,`rating_neutral`,`rating_bad`,`rating_name`,`rating_year`,`rating_week`,`cash_balance`,`cash_spend`)

values

(63741, 1, null, 100, null, 1, null, null, 'poland', 2010, 15, null, null),

(63742, 1, null, 101, null, 1, null, null, 'poland', 2010, 15, null, null),

(1, 2, null, 102, null, 1, null, null, 'poland', 2010, 15, null, null),

(63743, 3, null, 103, null, null, 1, null, 'poland', 2010, 15, null, null),

(63744, 4, null, 104, null, null, null, 1, 'poland', 2010, 15, null, null),

(63745, 1, null, 105, null, 1, null, null, 'poland', 2010, 15, null, null),

(63746, 1, null, 106, null, null, 1, null, 'poland', 2010, 15, null, null),

(63747, 5, null, 100, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63748, 5, null, 101, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63749, 2, null, 102, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63750, 3, null, 103, null, null, 1, null, 'ireland', 2010, 15, null, null),

(63751, 4, null, 104, null, null, null, 1, 'ireland', 2010, 15, null, null),

(63752, 1, null, 105, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63753, 1, null, 106, null, null, 1, null, 'ireland', 2010, 15, null, null),

(63754, 1, null, 100, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63755, 1, null, 101, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63756, 2, null, 102, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63757, 34, null, 103, null, null, 1, null, 'ireland', 2010, 15, null, null),

(63758, 34, null, 104, null, null, null, 1, 'ireland', 2010, 15, null, null),

(63759, 34, null, 105, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63760, 34, null, 106, null, null, 1, null, 'ireland', 2010, 15, null, null),

(63761, 21, null, 100, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63762, 21, null, 101, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63763, 21, null, 102, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63764, 21, null, 103, null, null, 1, null, 'ireland', 2010, 15, null, null),

(63765, 4, null, 104, null, null, null, 1, 'ireland', 2010, 15, null, null),

(63766, 1, null, 105, null, 1, null, null, 'ireland', 2010, 15, null, null),

(63767, 1, null, 106, null, null, 1, null, 'ireland', 2010, 15, null, null),

(63768, 1, null, 100, null, 1, null, null, 'france', 2010, 15, null, null),

(63769, 1, null, 101, null, 1, null, null, 'france', 2010, 15, null, null),

(63770, 2, null, 102, null, 1, null, null, 'france', 2010, 15, null, null),

(63771, 3, null, 103, null, null, 1, null, 'france', 2010, 15, null, null),

(63772, 4, null, 104, null, null, null, 1, 'france', 2010, 15, null, null);

mysql 隨機分組 Mysql 分組選擇

mysql 分組選擇與group concat 在其他的資料庫中我們遇到分組選擇的問題時,比如在分組中計算前10名的平均分 我們可以使用row number over 比較方便的得到。但是在mysql中,問題就被拋了出來,因為mysql並沒有乙個rownum內建資料元組訪問方式。在這裡我提供三種方式...

mysql分組排名 mysql分組排名

1.建立表並寫入測試資料 create table tb rank score city varchar 20 score int insert into tb rank score values sz 89 insert into tb rank score values sz 76 insert...

mysql分組查詢效能優化 MySQL分組查詢優化

我有三個表 類別,文章和article events,具有以下結構 categories id,name 100,000 rows articles id,category id 6000 rows article events id,article id,status id 20,000 rows...