花果山传奇:SQL语句的问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 08:01:06
我想从数据库选择a=1和a=2和a=3的记录,然后在这结果中选择b=1的10记录B=2的10条记录,B=3的10条记录,这样的SQL语句怎么写?一条语句能完成吗?
一张表里
我做随机抽题的程序,表结构:classid表示科室名,tmtype表示题型(a表示选择,B表示判断题,C表示多选题),现在就是想随机抽公共题和对应的科室题,公共题50道,科室题50道,然后再100除于3(三种题型),每个题型抽取相对数的题

select top 10 * from (select * from tablename where a in (1,2,3)) as tempTable where b=1
union
select top 10 * from (select * from tablename where a in (1,2,3)) as tempTable where b=2
union
select top 10 * from (select * from tablename where a in (1,2,3)) as tempTable where b=3

--tableName就是你要查询的表名,其他不用更改

不知道你使用的是什么数据库, 在ORACLE中好像好像要用rownum

select * from (select * from tablename where a in (1,2,3)) as tempTable where b=1 and rownum<11
union
select * from (select * from tablename where a in (1,2,3)) as tempTable where B=2 and rownum<11
union
select * from (select * from tablename where a in (1,2,3)) as tempTable where B=3 and rownum<11

同意楼上的

请问是在几个表里进行?