Home >Database >Mysql Tutorial >sql 去除重复记录的语句

sql 去除重复记录的语句

WBOY
WBOYOriginal
2016-06-07 17:47:08983browse

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 行受影响)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn