SQL 表里 按条件随机 查询出不重复的记录 然后指定某列出现值重复的次数上限

2025-02-25 14:15:01
推荐回答(2个)
回答1:

SELECT top 10 * FROM 专家抽取 WHERE 申报专业大专业 LIKE '%8%'
and name in (
SELECT name FROM 专家抽取 WHERE 申报专业大专业 LIKE '%8%'
group by name having count(name)=1
)
and department in(
SELECT name FROM 专家抽取 WHERE 申报专业大专业 LIKE '%8%'
group by department having count(department)<=2
)
order by NewID()
这不重复记录是指每次查询时都不重复呢还是一次查询中的数据不重复呢

回答2:

select top 10 * from (select distinct 姓名,工作部门,申报专业大专业 from 专家抽取)temp WHERE 申报专业大专业 LIKE '%8%' order by NewID()