Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL:如何随机选择并按最高顺序排序_Mysql_Sql_Random - Fatal编程技术网

Mysql SQL:如何随机选择并按最高顺序排序

Mysql SQL:如何随机选择并按最高顺序排序,mysql,sql,random,Mysql,Sql,Random,我有一个如下所示的模式: 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

我有一个如下所示的模式:

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: 问题是,我的查询没有随机选择5行并按点排序

这是因为在给定的查询中,您使用的是ORDERBY子句。
select id, name, point from users Order by point desc, rand() LIMIT 5

编辑

select id,name,pont from
(select id, name, point from users Order by rand() LIMIT 5)temp
order by point desc
注意:数据库中应该没有名为temp的表(即,因为您在别名中使用它)


Order by rand()
当您有一个巨大的表时,这将是一个缓慢的解决方案。@SaVaFa:对rand()有什么建议吗?@Redbox,请参阅:@SaVaFa,谢谢,但查询太复杂了。我有30k行,这是我使用rand()的结果:显示第0-27行(总共28行,查询耗时0.1296秒)
select id,name,pont from
(select id, name, point from users Order by rand() LIMIT 5)temp
order by point desc
select id,name,point from
(select id, name, point from users Order by rand()  
LIMIT 5) abc
order by point desc;