如何利用sql语句查重

如何利用sql语句查重

问:如何用sql语句查询重复记录
  1. 答:select
    *
    from
    log
    as
    a
    ,(select
    message
    from
    log
    group
    by
    message
    having
    count(*)>1)
    b
    where
    a.message
    =b.message
    这么写会比你的写法效率高一些,不过暂时想不出可以大幅度改善性能的写法。
    我的语句是联接,而楼主的查询是嵌套子查询。
    SQL
    SERVER帮助中说的很明白:在一些必须检查存在性的情况中,使用联接会产生更好的性能。否则,为确保消除重复值,必须为外部查询的每个结果都处理嵌套查询。所以在这些情况下,联接方式会产生更好的效果。
问:sql语句如何查询重复数据
  1. 答:阳光上的桥
    你这个不行的
    一般ID不会重复所有
    count(*)>1
    还能查
    如果重复的是多个的
    比如名称
    aaa重复3次
    bbb重复2次
    那么你的代码就会把aaa和bbb全部读出来
    而不是
    重复最多

    我是这样想的,比如说重复的是名称name
    则查询按名称分组的按统计排序的第一条(倒序,数字越大的排前面),这样求出的名称就是重复最多的名称。
    select
    top
    1
    name
    from
    a1
    group
    by
    name
    order
    by
    count(*)
    desc
问:如何用一条SQL语句查询数据库重复记录
  1. 答:方法如下:
    select * from 你的表名
    a where id=(select min(id) from 你的表名 whereitem_id=a.item_id)
    在查询之前先把数据库表中的第一行复制到sid里在去,然后让sid和下面的每一行进行比较
    取所有相同的行的最小的一下,也可以取最大的,结果是一样的。
    这样让所有的行都比较不就得到不重复的数据了。
  2. 答:重复的网址的记录
    select 网址字段
    from 表
    group by 网址字段
    having count(*)>1
    补充问题,如果判断A表中数据不在B表的对比条件在一个或一个以上,用left join
    写个例子
    insert into B(字段...)
    select a.字段...
    from a left join b
    on a.字段1=b.字段1 and a.字段2=b.字段2 ....
    where b.字段1 is null
  3. 答:if not exists(select * from B where 条件)
    insert into B...
    如果B表不存在指定数据则插入,否则不插入
  4. 答:你是要问什么?是要问什么数据库?数据库某一张表中的某个字段重复?还是整条记录除了ID以外重复?
问:怎么利用SQL语句查询数据库中具体某个字段的重复行
  1. 答:可以利用分组和count函数来进行统计,大致思想如下:
    select 列名, count(列名) from 表名
    group by 列名
    having count(列名)>1这样统计出来的是有重复的行的重复数量。
  2. 答:假设有张基础表
    EMP
    里面有sal,id,ename
    等等
    然后要查工资sal
    都是3000的雇员信息
    可以这样写:
    select
    id,ename,sal
    from
    emp
    group
    by
    sal
    having
    count(sal)>1;
    就能查出工资sal字段
    重复的所有职员信息了!!
  3. 答:我一般用这个:
    假设怀疑重复的字段名为SeriNo,
    select
    *
    from
    [tablename]
    group
    by
    SeriNo
    having
    count(SeriNo)<>1
问:使用sql server 怎么查重复数据
  1. 答:1、最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:
    select name from emp group by name having count(*)>1
    所有名字重复人的记录是:
    select * from emp
    where name in (select name from emp group by name having count(*)>1)
    2、稍微再聪明一点,就会想到,如果对每个名字都和原表进行比较,大于2个人名字与这条记录相同的就是合格的 ,就有:
    select * from emp
    where (select count(*) from emp e where e.name=emp.name) >1
  2. 答:SELECT *,
    count(*) C
    FROM 表明
    GROUP BY 字段
    ORDER C DESC
问:SQL如何查询出来重复的语句
  1. 答:select
    distinc
    列名
    from
    表名加上此关键字可以将重复的数据以一条显示出来!
问:SQL查重语句
  1. 答:按手机号码分组查询。count(*)大于1的就是有重复的手机号码。
  2. 答:可以看看数据库嵌套查询之类;
    select * from B where phonenumber in(select phonenumber from A);
    其中的* 可以改成你要的名字,住址之类;
    A,B代表两个表;
    phonenumber代表两表中存电话号码的列名;
    试一下。
  3. 答:select b.姓名,b.地址,b.手机号 from a,b where a.手机号=b.手机号
问:关于SQL查询重复语句
  1. 答:Select distinct post_content,post_title
    From wp_posts
  2. 答:select distinct post_content,post_title from wp_posts group by post_content,post_title
  3. 答:select * from table1 b where bill_id=(select top 1 bill_id from table1 a where exists(select top 1 column1 from table1 where column1=a.column1 group by column1 having count(*)>1 order by column1) and b.column1=a.column1) order by column1
    SQL语句查询有部份字段重复的第一条数据
  4. 答:使用临时表
    求出重复记录
    select userid from t_name group by userid having count(*) > 1
    求出不重复记录
    select distinct * into #a1 from t_name where userid in
    select userid from t_name group by userid having count(*) > 1
    删除重复记录
    从#a1取出不重复记录
  5. 答:你的图中post_content全一样,post_title有两种,这算不算重复呀?我按post_content重复就查找出来。
    查重:
    select post_content,post_title from wp_posts group by post_content having count(post_content)>1
    删除要先将数据查询到临时表中,然后将原表清空,在追加回来。
如何利用sql语句查重
下载Doc文档

猜你喜欢