Home > Database > Mysql Tutorial > body text

SQL Server查询语句过滤重复的数据

WBOY
Release: 2016-06-07 16:22:02
Original
1523 people have browsed it

情况一:表中存在完全重复的的数据,即所有字段内容都是相同的 create table # (用户ID int, 姓名 varchar(10), 年龄 int ) insert into # select 111, '张三', 26 union all select 222, '李四', 25 union all select 333, '王五', 30 union all select 111

   情况一:表中存在完全重复的的数据,,即所有字段内容都是相同的

  create table #

  (用户ID int, 姓名 varchar(10), 年龄 int )

  insert into #

  select 111, '张三', 26 union all

  select 222, '李四', 25 union all

  select 333, '王五', 30 union all

  select 111, '张三', 26

  方法: select distinct * from #

  情况2:表中存在部分数据重复的字段,即 重复数据中至少有一个字段不重复create table #(用户ID int, 姓名 varchar(10), 年龄 int, 日期 DateTime )insert into #select 111, '张三', 26 2010-02-23 union allselect 222, '李四', 25 2010-03-13 union allselect 333, '王五', 30 2011-03-25 union allselect 111, '张三', 26 2011-07-07方法:--当两条重,取日期大的一条

  select * from t a

  where not exists (select 1 from t where a.用户ID=用户ID a.姓名=姓名 and 日期>a.日期)

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!