猿问

MYSQL pickup Random row after order by

我想在 order by 之后选择一个随机行。


我的结果有时会返回具有相同值的 2 行。所以我想做的是我想在返回的相同行之间随机选择 1 行。


在下面的示例中,我想在返回的 2 个相同行之间返回 1 行。


谢谢。


select nondox, account, id 

    from rates2 where weight='32' and country_code='US' 

         and service like '%INT%' 

    order by nondox,account

上面查询的结果是


nondox  account   id 

276.16  610661731 25805209

276.16  610798714 2108989

391.68  610662766 1281799


慕容708150
浏览 105回答 2
2回答

白衣非少年

能否提供更多细节?这将消除类似的 nondox 值,它不是完全随机的,但它应该有效。  select account, id , nondox from rates2 where weight='32' and country_code='US' and service like '%INT%' group by nondox order by nondox,account

人到中年有点甜

您可以使用函数 RAND() 为表中的每一行生成一个随机值SELECT * FROM table_nameORDER BY RAND()LIMIT 1;
随时随地看视频慕课网APP
我要回答