用关键字 stinct,select stinct 字段,是不重复的意思。代码的实例如下:
查询order_id和loan_lind两个字段相同的记录:
select distinct a.order_preview_id, a.order_id, a.loan_kind
from ddk_order_preview_info a
join ddk_order_preview_info b
on a.order_preview_id != b.order_preview_id
where a.order_id = b.order_id and a.loan_kind = b.loan_kind
扩展资料
SQL数据库查询出一张表中重复的数据,按某个字段来查找的实例:
例如表名为Course:
需要查询出name的重复,解答如下:
补充:
如:查询每个姓名出现大于2次,SQL如下
SELECT COUNT(NAME) as '出现次数', NAME FROM 表名
GROUP BY NAME HAVING count(NAME) >2 ORDER BY 出现次数 DESC
参考资料来源:MySql官方网站-MySQL 8.0参考手册-13.2.10 SELECT语法
查询重复记录id
select user_id from user_info group by user_id having count(user_id) >1
--查询指定条件下的重复次数--测试数据
with tabname(id,name) as (
select 1,'name1' union all
select 1,'name1' union all
select 1,'name1' union all
select 1,'name2' union all
select 1,'name2' union all
select 1,'name3' union all
select 2,'name1' union all
select 2,'name1' union all
select 2,'name2' union all
select 2,'name3' union all
select 3,'name1')
select id,(name1+name2+name3) as 重复次数,name1,name2,name3 from(
select id,name from tabname
) as a
pivot(
count(name)
for
name in (name1,name2,name3)
) as b
结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)