
1、创建测试表,插入数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
create table product
(id int,
name varchar(10),
totol int)
insert into product values (1,'香蕉',100)
insert into product values (2,'橘子',67)
insert into product values (3,'葡萄',89)
insert into product values (4,'苹果',235)
insert into product values (5,'香蕉',77)
insert into product values (6,'芒果',34)
insert into product values (7,'葡萄',78)
insert into product values (8,'梨',24)
表中数据如:
2、如果查询name列有重复的数据,可执行sql语句:
1
select * from product where name in (select name from product group by name having COUNT(*)>1)
说明:查询的结果就是香蕉和葡萄在表中是有重复的,要把香蕉和葡萄的所有记录都查询出来,结果如图:
方法一:可以通过group by 进行分组。\x0d\x0asql:select username,count(username) from tablename grop by username\x0d\x0a解释:以上sql就是通过分组函数读取出tablename表中username的值和每个不同值的统计个数。\x0d\x0a方法二:可以通过distinct函数 进行去重查询。\x0d\x0asql:select distinct username from tablename\x0d\x0a解释:本sql就是查询出所有的tablename表中的username值(不重复)。1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count (peopleId) >1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people where peopleId in (select peopleId from people group by peopleId having count (peopleId) >1)and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
扩展资料
FROM子句指定SELECT语句查询及与查询相关的表或视图。在FROM子句中最多可指定256个表或视图,它们之间用逗号分隔。
在FROM子句同时指定多个表或视图时,如果选择列表中存在同名列,这时应使用对象名限定这些列所属的表或视图。
例如在usertable和citytable表中同时存在cityid列,在查询两个表中的cityid时应使用下面语句格式加以限定:
SELECTusername,citytable.cityid
FROMusertable,citytable
WHEREusertable.cityid=citytable.cityid
在FROM子句中可用以下两种格式为表或视图指定别名:
表名 as 别名
表名 别名
参考资料:百度百科 SELECT语句
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)