最近在整理自己的word press博客,像让其中一个页面展示随机查询的几篇图片日志,网上搜了下来sql的随机查询方式,很实用,收藏下来了:
sql Server:
Select TOP N * From TABLE Order By NewID()
view plaincopy to clipboardprint?
Select TOP N * From TABLE Order By NewID()
Select TOP N * From TABLE Order By NewID()
NewID()函数将创建一个 uniqueidentifier 类型的唯一值。上面的语句实现效果是从Table中随机读取N条记录。
Access:
Select TOP N * From TABLE Order By Rnd(ID)
view plaincopy to clipboardprint?
Select TOP N * From TABLE Order By Rnd(ID)
Select TOP N * From TABLE Order By Rnd(ID)
Rnd(ID) 其中的ID是自动编号字段,可以利用其他任何数值来完成,比如用姓名字段(UserName)
Select TOP N * From TABLE Order BY Rnd(Len(UserName))
view plaincopy to clipboardprint?
Select TOP N * From TABLE Order BY Rnd(Len(UserName))
Select TOP N * From TABLE Order BY Rnd(Len(UserName))
Select * From TABLE Order By Rand() Limit 10
view plaincopy to clipboardprint?
Select * From TABLE Order By Rand() Limit 10
Select * From TABLE Order By Rand() Limit 10
postgresql:
select * from glxt022 order by random() limit 5
view plaincopy to clipboardprint?
select * from glxt022 order by random() limit 5
不过后来发现word press模板没必要直接写sql的,从官方看了下,它基本上都包装得很接口模块化了,很强大,只要在 query_posts
加这么一个参数: