当前位置:Gxlcms > mysql > sql去除重复记录的语句

sql去除重复记录的语句

时间:2021-07-01 10:21:17 帮助过:42人阅读

sql 去除重复记录的语句
表A:
ID,Name
表B:
ID,AID,Value

select case when a.name='ccc' then null else a.name end name,b.value from 表A a,表B b where a.ID=b.AID


select nullif(a.name,'ccc') name ,b.value from 表A a,表B b where a.ID=b.AID

生成测试数据表: [tb]

IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([name] [nvarchar](10),[value] [int])
INSERT INTO [tb]
SELECT 'aaa','1' UNION ALL
SELECT 'bbb','2' UNION ALL
SELECT 'ccc','3' UNION ALL
SELECT 'ccc','4' UNION ALL
SELECT 'ccc','5'

-->SQL查询如下:

SELECT name = CASE [value]
WHEN (
SELECT MIN([value])
FROM tb
WHERE name = t.name
) THEN name
ELSE ''
END, [value]
FROM [tb] t
/*
name value
---------- -----------
aaa 1
bbb 2
ccc 3
4
5

(5 行受影响)

人气教程排行