MySQL随机取一条数据 取指定条数的数据

原创
2014/07/23 08:43
阅读数 196

第一个:最简便 性能最差的:

SELECT * FROM tb_goods ORDER BY RAND() limit 3;


第二个:性能比较好但是只能出来一条的:

SELECT * 
FROM `goods` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(goods_id) FROM `goods`)-(SELECT MIN(goods_id) FROM `goods`))+(SELECT MIN(goods_id) FROM `goods`)) AS id) AS t2 
WHERE t1.goods_id >= t2.id
ORDER BY t1.goods_id LIMIT 1;

http://database.51cto.com/art/201010/229981.htm

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部