SQL code
create table lk3 ( id int not null auto_increment primary key, `name` char(20), grade int, goldUser int ) engine=myisam; insert into lk3 (`name`,grade,goldUser) values ('jack',300, 10 ), ('mach', 200, 10 ), ('lich', 100 ,10 ), ('rock', 1, 0 ), ('mick', 1 ,10 ), ('kight', 0 ,10 ), ('babaya', 0, 0 ), ('kano', 0, 10);
(SELECT * FROM lk3 where grade != 1 order by grade desc,goldUser limit 3) union all (select * from lk3 where grade = 1 order by rand() limit 2)
-- 如果这里没有limit 2的话,grade为1 的两个用户的结果永远都不可能和预期一样。 union all (select * from lk3 where grade != 1 order by grade desc,goldUser limit 3,3)
结果1:
query result(8 records) id name grade goldUser 1 jack 300 10 2 mach 200 10 3 lich 100 10 4 rock 1 0 5 mick 1 10 7 babaya 0 0 6 kight 0 10 8 kano 0 10
结果2:
query result(8 records) id name grade goldUser 1 jack 300 10 2 mach 200 10 3 lich 100 10 5 mick 1 10 4 rock 1 0 7 babaya 0 0 6 kight 0 10 8 kano 0 10
|