sql in 一个字段多条件随机查询

时间:2022-02-10 19:22:39
表A
ID  sort1  num1  data1
1   类型1   数据  数据
2   类型1   数据  数据
3   类型2   数据  数据
4   类型3   数据  数据
5   类型1   数据  数据
6   类型1   数据  数据
7   类型2   数据  数据
8   类型3   数据  数据
9   类型3   数据  数据
10  类型3   数据  数据

表B
ID  sort1
1   类型1
2   类型2
3   类型3
4   类型4


实现功能
根据B表的类型,查询A表,并且每个类型随机查询TOP 10条记录,最终生成一个表

SELECT top 50 * FROM user_exam  WHERE  ttx in (SELECT Tx FROM user_tx ) order by newid()
//这个语句不能做到限制每个类型的记录数



用UNION 方法倒是可以实现,但是这个方法比较复杂,也无法实现随机
SELECT top 10 * FROM user  WHERE  sort1 ='类型1'  UNION
SELECT top 10 * FROM user  WHERE  sort1 ='类型2' UNION 
SELECT top 10 * FROM user  WHERE  sort1 ='类型3' UNION 
SELECT top 10 * FROM user  WHERE  sort1 ='类型4'

请高手赐教,如何简化此语句。


12 个解决方案

#1


如果是top 10,就不是随机的了.先给你top 10 的.

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by id) and sort1 in (select sort1 from b)

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by id desc) and sort1 in (select sort1 from b)

#2


--如果想随机,试试

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by newid()) and sort1 in (select sort1 from b)

#3


引用 2 楼 dawugui 的回复:
SQL code--如果想随机,试试

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by newid()) and sort1 in (select sort1 from b)

试过了,不成功,不好意思.

#4


睡觉了,老D,由你来搞定了。

#5




select * from (
select *,CN = row_number() over(partition by sort1 order by newid()) 
from TableA 
) as B
where CN <= 10

#6


/*
ID  sort1  num1  data1 
1  类型1  数据  数据 
2  类型1  数据  数据 
3  类型2  数据  数据 
4  类型3  数据  数据 
5  类型1  数据  数据 
6  类型1  数据  数据 
7  类型2  数据  数据 
8  类型3  数据  数据 
9  类型3  数据  数据 
10  类型3  数据  数据 
*/
if object_id('tb') is not null drop table tb
create table tb
(
  ID int,
  sort1 varchar(20),
  num1 varchar(20),
  data1 varchar(20)
)
insert into tb select 1,'类型1','数据','数据'
union all select 2,'类型1','数据','数据'
union all select 2,'类型2','数据','数据'
union all select 2,'类型3','数据','数据'
union all select 2,'类型1','数据','数据'
union all select 2,'类型1','数据','数据'
union all select 2,'类型2','数据','数据'
union all select 2,'类型3','数据','数据'
union all select 2,'类型3','数据','数据'
union all select 2,'类型3','数据','数据'

select * from (select *,row_number() over (partition by sort1 order by num1) rank from tb) tt
where rank<=10 order by sort1
ID          sort1                num1                 data1                rank
----------- -------------------- -------------------- -------------------- --------------------
1           类型1                  数据                   数据                   1
2           类型1                  数据                   数据                   2
2           类型1                  数据                   数据                   3
2           类型1                  数据                   数据                   4
2           类型2                  数据                   数据                   1
2           类型2                  数据                   数据                   2
2           类型3                  数据                   数据                   1
2           类型3                  数据                   数据                   2
2           类型3                  数据                   数据                   3
2           类型3                  数据                   数据                   4

(10 行受影响)

#7


楼主的Union语句已经是简化的了,再简化也没有了。

要实现完全随机不好办,取top 10凑合一下

#8


没看明白,难道这样的表结构有问题,可否考虑把表A的数据根据表B的数据分解成多个表, 查询的每个表随机查询10条记录,最好在合在一个表中? 但这样生成表使用起来比较麻烦。

#9


我觉得我5楼的代码可以实现了呀。每个类取10个啊 

#10


引用 5 楼 zoffor 的回复:
SQL code

select * from (
select *,CN = row_number() over(partition by sort1 order by newid()) 
from TableA 
) as B
where CN <= 10



'row_number' 不是可以识别的 函数名。

#11


引用 10 楼 incsun 的回复:
'row_number' 不是可以识别的 函数名。


row_number()在2005里有,楼主用的是2000?

#12


try:
SELECT top 50 * FROM 表A a   WHERE  sort1 in (SELECT sort1 FROM 表B ) 

and id in (select top 10  id from 表A where sort1=a.sort1 order by newid() ) 

#1


如果是top 10,就不是随机的了.先给你top 10 的.

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by id) and sort1 in (select sort1 from b)

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by id desc) and sort1 in (select sort1 from b)

#2


--如果想随机,试试

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by newid()) and sort1 in (select sort1 from b)

#3


引用 2 楼 dawugui 的回复:
SQL code--如果想随机,试试

select t.* from a t where id in (select top 10 id from a where sort1 = t.sort1 order by newid()) and sort1 in (select sort1 from b)

试过了,不成功,不好意思.

#4


睡觉了,老D,由你来搞定了。

#5




select * from (
select *,CN = row_number() over(partition by sort1 order by newid()) 
from TableA 
) as B
where CN <= 10

#6


/*
ID  sort1  num1  data1 
1  类型1  数据  数据 
2  类型1  数据  数据 
3  类型2  数据  数据 
4  类型3  数据  数据 
5  类型1  数据  数据 
6  类型1  数据  数据 
7  类型2  数据  数据 
8  类型3  数据  数据 
9  类型3  数据  数据 
10  类型3  数据  数据 
*/
if object_id('tb') is not null drop table tb
create table tb
(
  ID int,
  sort1 varchar(20),
  num1 varchar(20),
  data1 varchar(20)
)
insert into tb select 1,'类型1','数据','数据'
union all select 2,'类型1','数据','数据'
union all select 2,'类型2','数据','数据'
union all select 2,'类型3','数据','数据'
union all select 2,'类型1','数据','数据'
union all select 2,'类型1','数据','数据'
union all select 2,'类型2','数据','数据'
union all select 2,'类型3','数据','数据'
union all select 2,'类型3','数据','数据'
union all select 2,'类型3','数据','数据'

select * from (select *,row_number() over (partition by sort1 order by num1) rank from tb) tt
where rank<=10 order by sort1
ID          sort1                num1                 data1                rank
----------- -------------------- -------------------- -------------------- --------------------
1           类型1                  数据                   数据                   1
2           类型1                  数据                   数据                   2
2           类型1                  数据                   数据                   3
2           类型1                  数据                   数据                   4
2           类型2                  数据                   数据                   1
2           类型2                  数据                   数据                   2
2           类型3                  数据                   数据                   1
2           类型3                  数据                   数据                   2
2           类型3                  数据                   数据                   3
2           类型3                  数据                   数据                   4

(10 行受影响)

#7


楼主的Union语句已经是简化的了,再简化也没有了。

要实现完全随机不好办,取top 10凑合一下

#8


没看明白,难道这样的表结构有问题,可否考虑把表A的数据根据表B的数据分解成多个表, 查询的每个表随机查询10条记录,最好在合在一个表中? 但这样生成表使用起来比较麻烦。

#9


我觉得我5楼的代码可以实现了呀。每个类取10个啊 

#10


引用 5 楼 zoffor 的回复:
SQL code

select * from (
select *,CN = row_number() over(partition by sort1 order by newid()) 
from TableA 
) as B
where CN <= 10



'row_number' 不是可以识别的 函数名。

#11


引用 10 楼 incsun 的回复:
'row_number' 不是可以识别的 函数名。


row_number()在2005里有,楼主用的是2000?

#12


try:
SELECT top 50 * FROM 表A a   WHERE  sort1 in (SELECT sort1 FROM 表B ) 

and id in (select top 10  id from 表A where sort1=a.sort1 order by newid() )