Question:
表:article
字段:
id 主键
fid 所属类别id
title 文章主题
同一个fid下有很多文章
怎么用一条SQL语句把每个不同的类别(fid)各随机取出一条记录来?
Answer:
select * from (select * from article order by rand()) as anothername group by fid;
<iframe frameborder="0" id="gn_notemagic" style="position: absolute; display: block; opacity: 0.7; z-index: 500; width: 17px; height: 21px; top: 172px; right: 226px;" src="http://www.google.com/gn/static_files/blank.html"></iframe>
表:article
字段:
id 主键
fid 所属类别id
title 文章主题
同一个fid下有很多文章
怎么用一条SQL语句把每个不同的类别(fid)各随机取出一条记录来?
Answer:
select * from (select * from article order by rand()) as anothername group by fid;
<iframe frameborder="0" id="gn_notemagic" style="position: absolute; display: block; opacity: 0.7; z-index: 500; width: 17px; height: 21px; top: 172px; right: 226px;" src="http://www.google.com/gn/static_files/blank.html"></iframe>
SQL随机取记录
本文介绍了一种使用SQL语句从不同类别中随机选取一条记录的方法。通过子查询结合ORDER BY RAND()与GROUP BY语句实现需求。
213





