分组查找ACCESS数据库,每组随机取N条记录并按照id排序
ACCESS数据库结构为:表名ff。字段有:id(自动编号),ff(文本),aa(文本),bb(文本),cc(文本)
以下是ACCESS导出的文本文件:
1,"aa","a1","a1","a1"
2,"aa","a2","a2","a2"
3,"aa","a3","a3","a3"
4,"aa","a1","a1","a1"
5,"aa","a2","a2","a2"
6,"aa","a3","a3","a3"
7,"bb","b4","b1","b1"
8,"bb","b5","b2","b2"
9,"bb","b6","b3","b3"
10,"bb","b1","b1","b1"
11,"bb","b2","b2","b2"
12,"bb","b3","b3","b3"
13,"bb","b4","b1","b1"
14,"bb","b5","b2","b2"
15,"bb","b6","b3","b3"
SQL语句如下:
select * from (select top 2 * from ff where ff="aa" order by rnd(id))
UNION ALL (select top 2 * from ff where ff="bb" order by rnd(id))
ORDER BY id;
每组要出几条记录只要把每组top后的数据改成几就行了。