表
id type tent
1. 判断题 题干1
2.判断题 题干2
3.判断题 题干3
4.判断题 题干4
5.判断题 题干5
6.选择题 题干6
7.选择题 题干7
8.选择题 题干8
9.选择题 题干9
10.选择题 题干10想要的结果:随机选两道选择题。两道判断题
id type tent
1. 判断题 题干2
2.判断题 题干4
3.选择题 题干5
4.选择题 题干7
5.选择题 题干8
sql查询语句怎么写。最好。
谢谢。
id type tent
1. 判断题 题干1
2.判断题 题干2
3.判断题 题干3
4.判断题 题干4
5.判断题 题干5
6.选择题 题干6
7.选择题 题干7
8.选择题 题干8
9.选择题 题干9
10.选择题 题干10想要的结果:随机选两道选择题。两道判断题
id type tent
1. 判断题 题干2
2.判断题 题干4
3.选择题 题干5
4.选择题 题干7
5.选择题 题干8
sql查询语句怎么写。最好。
谢谢。
select top 2 where type = '判断题'
union all
select top 2 where type = '选择题'
[/code]
union all
select top 3 * from tb where type='选择题' order by newid()
from tb
where type='判断题' order by NEWID()
union all
select top 2 *
from tb
where type='选择题' order by NEWID()
union all
select top 3 * from tb where type='选择题' order by newid()
order by type,id
from (
select top 2 *
from tb
where type='判断题' order by NEWID()
union all
select top 2 *
from tb
where type='选择题' order by NEWID()
) V
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-29 15:30:08
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[type] varchar(6),[tent] varchar(6))
insert [tb]
select 1,'判断题','题干1' union all
select 2,'判断题','题干2' union all
select 3,'判断题','题干3' union all
select 4,'判断题','题干4' union all
select 5,'判断题','题干5' union all
select 6,'选择题','题干6' union all
select 7,'选择题','题干7' union all
select 8,'选择题','题干8' union all
select 9,'选择题','题干9' union all
select 10,'选择题','题干10'
--------------开始查询--------------------------
select * from
(select top 2 * from tb where type='判断题'
union all
select top 3 * from tb where type='选择题')t order by newid()
----------------结果----------------------------
/* id type tent
----------- ------ ------
7 选择题 题干7
8 选择题 题干8
6 选择题 题干6
1 判断题 题干1
2 判断题 题干2(5 行受影响)
*/
select * ,px=identity(int,1,1) into #t from tbselect * from #t t
where (select count(1) from #t where type=t.type and px<=t.px)<=2
aaa[/code]
秘密啊
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-29 15:30:08
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[type] varchar(6),[tent] varchar(6))
insert [tb]
select 1,'判断题','题干1' union all
select 2,'判断题','题干2' union all
select 3,'判断题','题干3' union all
select 4,'判断题','题干4' union all
select 5,'判断题','题干5' union all
select 6,'选择题','题干6' union all
select 7,'选择题','题干7' union all
select 8,'选择题','题干8' union all
select 9,'选择题','题干9' union all
select 10,'选择题','题干10'
--------------开始查询--------------------------
select
ROW_NUMBER() over(order by id) id,[TYPE],tent
from
(
select top 2 * from tb where type='判断题' order by NEWID()
union all
select top 3 * from tb where type='选择题' order by NEWID()
) t
----------------结果----------------------------
/* id TYPE tent
-------------------- ------ ------
1 判断题 题干1
2 判断题 题干3
3 选择题 题干8
4 选择题 题干9
5 选择题 题干10(5 行受影响)
*/
ROW_NUMBER() over(order by id) id,[TYPE],tent
from这一句的作用。请问。
在关键字 'order' 附近有语法错误。
第 1 行: ')' 附近有语法错误。
服务器: 消息 195,级别 15,状态 10,行 2
'ROW_NUMBER' 不是可以识别的 函数名。
服务器: 消息 156,级别 15,状态 1,行 5
在关键字 'order' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 8
第 8 行: ')' 附近有语法错误。
----看样子是2000的数据库,呵呵........row_number()函数没法整,让小F占了便宜!
----我来蹭点分,楼主!