表结构这样的,和上次一样:序号 f1 f2
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 a
7 3 b
8 3 c
9 3 d
现在问题是要对 f1 和f2 都随机排序,并且排序出来f1 字段值一样的要分组在一起,比如这样的效果:
序号 f1 f2
4 2 a
5 2 b
1 1 a
2 1 b
3 1 c
6 3 a
7 3 b
8 3 c
9 3 d
不知道能不能实现呢?我太菜了,想了很久没想出,希望大侠讲讲,谢谢!
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 a
7 3 b
8 3 c
9 3 d
现在问题是要对 f1 和f2 都随机排序,并且排序出来f1 字段值一样的要分组在一起,比如这样的效果:
序号 f1 f2
4 2 a
5 2 b
1 1 a
2 1 b
3 1 c
6 3 a
7 3 b
8 3 c
9 3 d
不知道能不能实现呢?我太菜了,想了很久没想出,希望大侠讲讲,谢谢!
解决方案 »
- 带With的更新如何写!应该比较简单,但请给出具体代码!谢谢
- 怎样查找表中的第N条记录?
- 高手帮我看看是否安全~
- HELP!高分
- 怎样写一条语句删除一个列上所有的约束?怎样写一条语句删除一个表中所有的约束?
- 如何停掉正在使用的数据库然后在启动它啊!如何停掉正在使用的数据库然后在启动它啊!在线等
- 大劫难,我用的是jdbc:odbc,sql server,显示数据时,必须按字段的先后顺序取值,否则会出错“无效的描述符索引”
- 编制了一个存储过程,但是有两处错误,不知道是哪里错了! 有兴趣的都来看看!
- 同志们好!帮个忙吧?
- 怎么实现三层乃至多层数据库开发?
- 查询问题
- sql2000数据库 log文件太大了!
insert @t values(1, 1, 'a')
insert @t values(2 , 1 , 'b')
insert @t values(3 , 1 , 'c')
insert @t values(4 , 2 , 'a')
insert @t values(5, 2, 'b')
insert @t values(6 , 3 , 'a')
insert @t values(7 , 3 , 'b')
insert @t values(8 , 3 , 'c')
insert @t values(9 ,3 ,'d')select * from @t order by f1,newid()
/*
ID f1 f2
----------- ----------- ----------
1 1 a
3 1 c
2 1 b
4 2 a
5 2 b
8 3 c
9 3 d
7 3 b
6 3 a
*/
declare @t table(ID int,f1 int,f2 varchar(10))
insert @t values(1, 1, 'a')
insert @t values(2 , 1 , 'b')
insert @t values(3 , 1 , 'c')
insert @t values(4 , 2 , 'a')
insert @t values(5, 2, 'b')
insert @t values(6 , 3 , 'a')
insert @t values(7 , 3 , 'b')
insert @t values(8 , 3 , 'c')
insert @t values(9 ,3 ,'d')select * from @t
order by newid()
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([序号] int,[f1] int,[f2] varchar(1))
insert [tb]
select 1,1,'a' union all
select 2,1,'b' union all
select 3,1,'c' union all
select 4,2,'a' union all
select 5,2,'b' union all
select 6,3,'a' union all
select 7,3,'b' union all
select 8,3,'c' union all
select 9,3,'d'
go
--select * from [tb]select a.序号,a.f1,a.f2
from tb a
,(select f1,rn=row_number() over(order by newid()) from tb group by f1) b
where a.f1=b.f1
order by b.rn,newid()
/*
序号 f1 f2
----------- ----------- ----
4 2 a
5 2 b
2 1 b
3 1 c
1 1 a
9 3 d
7 3 b
6 3 a
8 3 c(9 行受影响)
*/
declare @t table(ID int,f1 int,f2 varchar(10))
insert @t values(1, 1, 'a')
insert @t values(2 , 1 , 'b')
insert @t values(3 , 1 , 'c')
insert @t values(4 , 2 , 'a')
insert @t values(5, 2, 'b')
insert @t values(6 , 3 , 'a')
insert @t values(7 , 3 , 'b')
insert @t values(8 , 3 , 'c')
insert @t values(9 ,3 ,'d')select * from @t
order by F1,NewID() desc这样试试!
declare @t table(ID int,f1 int,f2 varchar(10))
insert @t values(1, 1, 'a')
insert @t values(2 , 1 , 'b')
insert @t values(3 , 1 , 'c')
insert @t values(4 , 2 , 'a')
insert @t values(5, 2, 'b')
insert @t values(6 , 3 , 'a')
insert @t values(7 , 3 , 'b')
insert @t values(8 , 3 , 'c')
insert @t values(9 ,3 ,'d')select * from @t
order by
cast((rand()*99+1) as numeric(19,6))%(f1+1)
--没算过几率,不过确实都有可能
declare @t table(ID int,f1 int,f2 varchar(10))
insert @t values(1, 1, 'a')
insert @t values(2 , 1 , 'b')
insert @t values(3 , 1 , 'c')
insert @t values(4 , 2 , 'a')
insert @t values(5, 2, 'b')
insert @t values(6 , 3 , 'a')
insert @t values(7 , 3 , 'b')
insert @t values(8 , 3 , 'c')
insert @t values(9 ,3 ,'d')select * from @t
order by
cast((rand()*99+1) as numeric(19,6))%(f1+10)这个几率就差不多了
-------------------------------------------> 测试时间:2009-07-27
--> 我的淘宝:http://shop36766744.taobao.com/--------------------------------------------------if object_id('[l8r]') is not null drop table [l8r]
create table [l8r]([序号] int,[f1] int,[f2] varchar(1))
insert [l8r]
select 1,1,'a' union all
select 2,1,'b' union all
select 3,1,'c' union all
select 4,2,'a' union all
select 5,2,'b' union all
select 6,3,'a' union all
select 7,3,'b' union all
select 8,3,'c' union all
select 9,3,'d'select * from l8rselect * from(
select *,flag=rand() from l8r where f1=1 union all
select *,rand() from l8r where f1=2 union all
select *,rand() from l8r where f1=3 )t
order by flag,newid()/*
序号 f1 f2 flag
----------- ----------- ---- -----------------------------------------------------
6 3 a 0.3455370372278721
9 3 d 0.3455370372278721
8 3 c 0.3455370372278721
7 3 b 0.3455370372278721
2 1 b 0.38022202824029583
3 1 c 0.38022202824029583
1 1 a 0.38022202824029583
5 2 b 0.6187124653788858
4 2 a 0.6187124653788858(所影响的行数为 9 行)
*/drop table l8r
select *,flag=rand() from l8r where f1=1 union all
select *,rand() from l8r where f1=2 union all
select *,rand() from l8r where f1=3 )t
order by flag,newid()