表中随机查询记录

时间:2022-07-25 23:24:10

从表topic中随机查询topictype为1和2的记录各10条,

select * from
(
 select * from
 (
  select top 10 topicid,topic,topictype,content,answer,author,addtime,itemid from cean_topic where topictype=1 order by newid()
 ) as a1
  union  all
 
 select * from
 (
  select top 10 topicid,topic,topictype,content,answer,author,addtime,itemid from cean_topic where topictype=2 order by newid()
 ) as a2
 
) as a3

 

使用union all进行查询结果的合并,

SQL数据库用上面语句需要对子查询语句列出详细的列名,而Access则不需要,