美文网首页
MYSQL 8 優化之09 (使用rand() 隨機函數)

MYSQL 8 優化之09 (使用rand() 隨機函數)

作者: 轻飘飘D | 来源:发表于2019-08-19 22:12 被阅读0次
    1. 使用 order by rand() 隨機排序
    root@127.0.0.1 : testdb【11:17:07】7 SQL->select * from city order by rand();
    +---------+-----------+------------+---------------------+
    | city_id | city_name | country_id | last_update         |
    +---------+-----------+------------+---------------------+
    |    1010 | 大阪      |       1003 | 2019-07-13 04:14:15 |
    |    1003 | 華盛頓    |       1001 | 2019-07-13 04:12:14 |
    |    1001 | 旧金山    |       1001 | 2019-07-13 04:05:14 |
    |    1004 | 波士頓    |       1001 | 2019-07-13 04:12:30 |
    |    1013 | 倫敦      |       1005 | 2019-07-13 04:16:00 |
    |    1009 | 東京      |       1003 | 2019-07-13 04:14:00 |
    |    1007 | 廣州      |       1002 | 2019-07-13 04:13:21 |
    |    1011 | 柏林      |       1004 | 2019-07-13 04:14:29 |
    |    1006 | 南京      |       1002 | 2019-07-13 04:13:12 |
    |    1008 | 珠海      |       1002 | 2019-07-13 04:13:28 |
    |    1012 | 汉堡      |       1004 | 2019-07-13 04:15:45 |
    |    1015 | 劍橋      |       1005 | 2019-07-13 04:17:36 |
    |    1002 | 紐約      |       1001 | 2019-07-13 04:11:58 |
    |    1005 | 北京      |       1002 | 2019-07-13 04:12:58 |
    |    1014 | 牛津      |       1005 | 2019-07-13 04:17:19 |
    +---------+-----------+------------+---------------------+
    
    1. 使用 order by rand() limit 3 隨機抽取3個樣本
    root@127.0.0.1 : testdb【11:17:12】8 SQL->select * from city order by rand() limit 3;
    +---------+-----------+------------+---------------------+
    | city_id | city_name | country_id | last_update         |
    +---------+-----------+------------+---------------------+
    |    1002 | 紐約      |       1001 | 2019-07-13 04:11:58 |
    |    1008 | 珠海      |       1002 | 2019-07-13 04:13:28 |
    |    1005 | 北京      |       1002 | 2019-07-13 04:12:58 |
    +---------+-----------+------------+---------------------+
    
    root@127.0.0.1 : testdb【11:17:26】9 SQL->select * from city order by rand() limit 3;
    +---------+-----------+------------+---------------------+
    | city_id | city_name | country_id | last_update         |
    +---------+-----------+------------+---------------------+
    |    1005 | 北京      |       1002 | 2019-07-13 04:12:58 |
    |    1014 | 牛津      |       1005 | 2019-07-13 04:17:19 |
    |    1012 | 汉堡      |       1004 | 2019-07-13 04:15:45 |
    +---------+-----------+------------+---------------------+
    

    相关文章

      网友评论

          本文标题:MYSQL 8 優化之09 (使用rand() 隨機函數)

          本文链接:https://www.haomeiwen.com/subject/vzmmdctx.html