求一sql语句:提取table1表中type=1的前十条记录和type=2的前十条记录
提取table1表中type=1的前十条记录和type=2的前十条记录,
使用一个select语句
谢谢
问题点数:20、回复次数:8Top
1 楼hsj20041004(光芒)回复于 2005-06-01 18:02:11 得分 5
select top 10 * from table1 where type=1 order by id
union
select top 10 * from table1 where type=2 order by id
Top
2 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 18:02:21 得分 10
select top 10 * from yourtable
where type=1
union all
select top 10 * from yourtable
where type=2Top
3 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 18:03:50 得分 0
1。union 连接的时候,消除重复的行。
1。而union all保留重复的行。Top
4 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 18:04:25 得分 0
Union
返回对两个集合进行 union 运算所生成的集合,可以保留重复的成员。
语法
Union(«Set1», «Set2»[, ALL])
替代语法 1
{«Set1», «Set2»}
替代语法 2
«Set1» + «Set 2»
注释
此函数返回 «Set1» 和 «Set2» 的 union 运算结果,并在默认情况下消除重复项。ALL 标志表示在并集中保留重复项。从尾部删除重复项。
也可以将逗号分隔的集合列表用括号括起来或使用 + 运算符,通过 union 算法合并集合。例如:
{USA.Children, CANADA.Children}
与
{USA.Children} + {CANADA.Children}
等同于
Union(USA.Children, CANADA.Children, ALL)
使用替代语法时始终保留重复的成员。
Top
5 楼zbyh331(我才刚上路耶!)回复于 2005-06-01 18:06:20 得分 0
select top 10 * from table1 where type=1
select top 10 * from table1 where type=2Top
6 楼seelancer(郁闷枪骑兵)回复于 2005-06-01 18:18:19 得分 0
回:duanduan1122(我要做老大!!!)
请教一个问题,为什么下面的查询会出错误(不能以 DISTINCT 方式选择 text、ntext 或 image 数据类型)
select top 10 * from table1 where type=1 order by id
union
select top 10 * from table1 where type=2 order by id
如果将*替换为指定的字段就可以,如果想使用*,必须加union all
这是为什么 谢谢谢谢Top
7 楼seelancer(郁闷枪骑兵)回复于 2005-06-01 19:07:08 得分 0
upTop
8 楼xiaonvjing(飞扬)回复于 2005-06-01 19:45:55 得分 5
select top 10 * from table1 where type=1
union all
select top 10 * from table1 where type=2 order by id desc
Top




