关于user表
mysql> select * from user;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 1 | aaaaaaaaaaaaaaaaaa |
| 2 | 螟ァ譏ッ荳ェ |
| 3 | 譏ッ蠕キ蝗ス |
| 4 | |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 7 | d |
| 8 | 螳俶婿 |
| 9 | ? |
+--------+--------------------+
9 rows in set (0.00 sec)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 3 | 譏ッ蠕キ蝗ス |
| 6 | 螢ォ螟ァ螟ォ |
| 8 | 螳俶婿 |
| 5 | 莠懶ス難ス・ |
| 2 | 螟ァ譏ッ荳ェ |
| 9 | ? |
| 1 | aaaaaaaaaaaaaaaaaa |
| 4 | |
| 7 | d |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 4 | |
| 1 | aaaaaaaaaaaaaaaaaa |
| 7 | d |
| 9 | ? |
| 6 | 螢ォ螟ァ螟ォ |
| 3 | 譏ッ蠕キ蝗ス |
| 5 | 莠懶ス難ス・ |
| 8 | 螳俶婿 |
| 2 | 螟ァ譏ッ荳ェ |
+--------+--------------------+
9 rows in set (0.02 sec)mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 2 | 螟ァ譏ッ荳ェ |
| 6 | 螢ォ螟ァ螟ォ |
| 8 | 螳俶婿 |
| 1 | aaaaaaaaaaaaaaaaaa |
| 9 | ? |
| 5 | 莠懶ス難ス・ |
| 3 | 譏ッ蠕キ蝗ス |
| 7 | d |
| 4 | |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 9 | ? |
| 4 | |
| 8 | 螳俶婿 |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 1 | aaaaaaaaaaaaaaaaaa |
| 2 | 螟ァ譏ッ荳ェ |
| 7 | d |
| 3 | 譏ッ蠕キ蝗ス |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by rand();
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 7 | d |
| 2 | 螟ァ譏ッ荳ェ |
| 9 | ? |
| 3 | 譏ッ蠕キ蝗ス |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 4 | |
| 1 | aaaaaaaaaaaaaaaaaa |
| 8 | 螳俶婿 |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by 2;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 4 | |
| 9 | ? |
| 1 | aaaaaaaaaaaaaaaaaa |
| 7 | d |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 2 | 螟ァ譏ッ荳ェ |
| 8 | 螳俶婿 |
| 3 | 譏ッ蠕キ蝗ス |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by 132456.12345;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 1 | aaaaaaaaaaaaaaaaaa |
| 2 | 螟ァ譏ッ荳ェ |
| 3 | 譏ッ蠕キ蝗ス |
| 4 | |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 7 | d |
| 8 | 螳俶婿 |
| 9 | ? |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by 132456;
ERROR 1054 (42S22): Unknown column '132456' in 'order clause'
mysql> select * from user;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 1 | aaaaaaaaaaaaaaaaaa |
| 2 | 螟ァ譏ッ荳ェ |
| 3 | 譏ッ蠕キ蝗ス |
| 4 | |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 7 | d |
| 8 | 螳俶婿 |
| 9 | ? |
+--------+--------------------+
9 rows in set (0.00 sec)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 3 | 譏ッ蠕キ蝗ス |
| 6 | 螢ォ螟ァ螟ォ |
| 8 | 螳俶婿 |
| 5 | 莠懶ス難ス・ |
| 2 | 螟ァ譏ッ荳ェ |
| 9 | ? |
| 1 | aaaaaaaaaaaaaaaaaa |
| 4 | |
| 7 | d |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 4 | |
| 1 | aaaaaaaaaaaaaaaaaa |
| 7 | d |
| 9 | ? |
| 6 | 螢ォ螟ァ螟ォ |
| 3 | 譏ッ蠕キ蝗ス |
| 5 | 莠懶ス難ス・ |
| 8 | 螳俶婿 |
| 2 | 螟ァ譏ッ荳ェ |
+--------+--------------------+
9 rows in set (0.02 sec)mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 2 | 螟ァ譏ッ荳ェ |
| 6 | 螢ォ螟ァ螟ォ |
| 8 | 螳俶婿 |
| 1 | aaaaaaaaaaaaaaaaaa |
| 9 | ? |
| 5 | 莠懶ス難ス・ |
| 3 | 譏ッ蠕キ蝗ス |
| 7 | d |
| 4 | |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by rand()*1000000000;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 9 | ? |
| 4 | |
| 8 | 螳俶婿 |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 1 | aaaaaaaaaaaaaaaaaa |
| 2 | 螟ァ譏ッ荳ェ |
| 7 | d |
| 3 | 譏ッ蠕キ蝗ス |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by rand();
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 7 | d |
| 2 | 螟ァ譏ッ荳ェ |
| 9 | ? |
| 3 | 譏ッ蠕キ蝗ス |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 4 | |
| 1 | aaaaaaaaaaaaaaaaaa |
| 8 | 螳俶婿 |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by 2;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 4 | |
| 9 | ? |
| 1 | aaaaaaaaaaaaaaaaaa |
| 7 | d |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 2 | 螟ァ譏ッ荳ェ |
| 8 | 螳俶婿 |
| 3 | 譏ッ蠕キ蝗ス |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by 132456.12345;
+--------+--------------------+
| userid | username |
+--------+--------------------+
| 1 | aaaaaaaaaaaaaaaaaa |
| 2 | 螟ァ譏ッ荳ェ |
| 3 | 譏ッ蠕キ蝗ス |
| 4 | |
| 5 | 莠懶ス難ス・ |
| 6 | 螢ォ螟ァ螟ォ |
| 7 | d |
| 8 | 螳俶婿 |
| 9 | ? |
+--------+--------------------+
9 rows in set (0.00 sec)mysql> select * from user order by 132456;
ERROR 1054 (42S22): Unknown column '132456' in 'order clause'
运行原理吧mysql> select rand();
+-------------------+
| rand() |
+-------------------+
| 0.832840707928725 |
+-------------------+
1 row in set (0.00 sec)
在ORDER BY语句中,不能使用一个带有RAND()值的列,原因是 ORDER BY 会计算列的多重时间。然而,可按照如下的随机顺序检索数据行: mysql> SELECT * FROM tbl_name ORDER BY RAND();ORDER BY RAND()同 LIMIT 的结合从一组列中选择随机样本很有用:
--------------------------------
不是很理解
查询应该是先查询结果集 然后在order by 排序
如果是这样的话order by的列就是个定值而不是随机制如果select * from user where id=ceil(rand()*10);
那么它是一行一行匹配如果有索引也是匹配多行
是随机RAND 函数是每行计算,与其它函数不同。
应该等价于
SELECT *,RAND() as px FROM tbl_name ORDER BY px;
from(
select id,rand() as crand from t2) t
order by crand;
排队的先后顺序我们可以按照他们的性别(bit 0 or 1)、名字(varchar)、身高(numeric)等等进行排序
mysql> select * from user order by rand();
+--------+-----------+
| userid | username |
+--------+-----------+
| 7 | d |
| 8 | 螳俶婿 |
| 2 | 螟ァ譏ッ荳ェ |
| 4 | |
| 9 | ? |
| 6 | 螢ォ螟ァ螟ォ |
| 5 | 莠懶ス難ス・ |
| 3 | 譏ッ蠕キ蝗ス |
+--------+-----------+
8 rows in set (0.05 sec)
结果集如上 其也是随机抽取一列的实现之一 但是如果每行计算的话 结果集是如何排序的
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | t2 | index | NULL | PRIMARY | 4 | NULL | 11 | Using index; Using temporary; Using filesort |
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
1 row in set (0.00 sec)
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | t2 | index | NULL | PRIMARY | 4 | NULL | 11 | Using index; Using temporary; Using filesort |
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
1 row in set (0.00 sec)mysql> explain select * from t2 order by id;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | t2 | index | NULL | PRIMARY | 4 | NULL | 11 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)mysql> explain select id,rand() from t2 order by id;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | t2 | index | NULL | PRIMARY | 4 | NULL | 11 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)mysql>
而max,min等函数则是列结果出来之后才计算!这个可以楼主按照 《understanding mysql internals》中对mysql的调试方法来看看到底那个值是怎么出来的!
rand()相当于生成一个伪列 然后按照伪劣进行排序
mysql> select username ,userid as Vatural from user order by Vatural;
+-----------+---------+
| username | Vatural |
+-----------+---------+
| 螟ァ譏ッ荳ェ | 2 |
| 譏ッ蠕キ蝗ス | 3 |
| | 4 |
| 莠懶ス難ス・ | 5 |
| 螢ォ螟ァ螟ォ | 6 |
| d | 7 |
| 螳俶婿 | 8 |
| ? | 9 |
+-----------+---------+
8 rows in set (0.00 sec)mysql> select username ,rand() as Vatural from user order by Vatural;
+-----------+--------------------+
| username | Vatural |
+-----------+--------------------+
| 螟ァ譏ッ荳ェ | 0.0555073358635486 |
| 莠懶ス難ス・ | 0.134616873352432 |
| ? | 0.185337211177998 |
| 螳俶婿 | 0.270653398028252 |
| | 0.371484097439315 |
| d | 0.389309936565636 |
| 螢ォ螟ァ螟ォ | 0.558632105177857 |
| 譏ッ蠕キ蝗ス | 0.907601214859263 |
+-----------+--------------------+
8 rows in set (0.00 sec)
+------+
| a |
+------+
| 1 |
| 10 |
+------+
2 rows in set (0.00 sec)sqlcli> select * from t1 order by 2;
ERROR 1054 (42S22): Unknown column '2' in 'order clause'
sqlcli> select * from t1 order by '2';
+------+
| a |
+------+
| 1 |
| 10 |
+------+
2 rows in set (0.01 sec)order by 2 = order by 第二个字段