3

我有一个如下所示的架构:

CREATE TABLE users 
    (
     id int auto_increment primary key, 
     name varchar(20), 
     point int(255)
    );

INSERT INTO users
(name, point)
VALUES
('Jack', 1),
('Rick', 5),
('Danny', 11),
('Anthony', 24),
('Barla', 3),
('James', 15),
('Melvin', 12),
('Orthon', 5),
('Kenny', 2),
('Smith', 30),
('Steven', 27),
('Darly', 45),
('Peter', 44),
('Parker', 66),
('Lola', 78),
('Jennifer', 94),
('Smart', 87),
('Jin', 64),
('David', 31),
('Jill', 78),
('Ken', 48),
('Martin', 19),
('Adrian', 20),
('Oliver', 16),
('Ben', 100);

我的 sql 是:

select id, name, point from users Order by point desc, rand() LIMIT 5

问题是,我的查询没有随机选择 5 行并按点排序。任何想法,如何解决它?这里是 sqlfiddle: http ://sqlfiddle.com/#!2/18f15/1

4

2 回答 2

7
select id,name,point from
(select id, name, point from users Order by rand()  
LIMIT 5) abc
order by point desc;

SQLFIDDLE

于 2013-05-10T06:24:05.880 回答
2

问题是,我的查询没有随机选择 5 行并按点排序。

这是因为在您给定的查询中,您使用的是ORDER BY子句。

select id, name, point from users Order by point desc, rand() LIMIT 5

尝试删除点 desc, inORDER BY子句

select id, name, point from users Order by rand() LIMIT 5

SQL 小提琴

编辑

select id,name,pont from
(select id, name, point from users Order by rand() LIMIT 5)temp
order by point desc

注意:您的数据库中应该no Table有名称temp(即,因为您正在使用它alias

于 2013-05-10T06:18:18.817 回答