问一个sql!
table1:
id name addrss
1 huang wuhan
1 huang wuhan
2 li beijing
2 zhang nanjing
2 hu guangzhou
3 wang shenzhen
=========
分别查出ID相同其他字段不同 (几个人ID重复了)
2 li beijing
2 zhang nanjing
2 hu guangzhou
id相同 其他字段也相同(一条记录重复录入了)
1 huang wuhan
谢谢
------解决方案----------------------是這個嗎?
分别查出ID相同其他字段不同 (几个人ID重复了)
select * from t
where id in(select id from t group by id having count(*)> 1)
group by id,name,addrss
having count(*)=1
id相同 其他字段也相同(一条记录重复录入了)
select * from t
group by id,name,addrss
having count(*)> 1
------解决方案--------------------CREATE TABLE [dbo].[table1](
[id] [int]NULL,
[name] [varchar](50) NULL,
[addrss] [varchar](50) NULL
)
INSERT INTO [dbo].[table1]
SELECT 1, 'huang ', 'wuhan ' UNION ALL
SELECT 1, 'huang ', 'wuhan ' UNION ALL
SELECT 2, 'li ', 'beijing ' UNION ALL
SELECT 2, 'zhang ', 'nanjing ' UNION ALL
SELECT 2, 'hu ', 'guangzhou ' UNION ALL
SELECT 3, 'wang ', 'shenzhen '
--分别查出ID相同其他字段不同
SELECT DISTINCT A.* FROM [dbo].[table1] A INNER JOIN [dbo].[table1] B
ON
A.ID=B.ID AND A.[NAME]+A.[addrss] <> B.[NAME]+B.[addrss]
--结果
2 hu guangzhou
2 li beijing
2 zhang nanjing
--id相同 其他字段也相同
SELECT * FROM [dbo].[table1] GROUP BY [id],[name],[addrss] HAVING COUNT(1)> 1
--结果
1 huang wuhan
DROP TABLE [dbo].[table1]
------解决方案--------------------playwarcraft(时间就像乳沟,挤挤还是有的) 正解
create table table11(id int,name varchar(20),addrss varchar(50))
insert into table11 select 1, 'huang ', 'wuhan '
union all select 1, 'huang ', 'wuhan '
union all select 2, 'li ', 'beijing '
union all select 2, 'zhang ', 'nanjing '
union all select 2, 'hu ', 'guangzhou '
union all select 3, 'wang ', 'shenzhen '
--分别查出ID相同其他字段不同 (几个人ID重复了)
select * from table11
where id in(select id from table11 group by id having count(*)> 1)
group by id,name,addrss
having