日期:2014-05-18  浏览次数:20485 次

SQL随机查询问题?
假如有三个字段
ID           TypeID             Content
1                 1                       aaa
2                 1                       bbb
3                 1                       ccc
4                 2                       ddd
5                 2                       eee
6                 3                       fff
7                 3                       ggg
8                 3                       hhh

请问:要是想在TypeID相同中随机选出一个ID,也就是说在TypeID分别等于1,2,3,的时候随机选出一个ID,怎么做?

------解决方案--------------------
select (select top 1 ID from tablename where typeid=a.typeid order by newid()) from (select distinct typeid from tablename) a