mysql查重复字段

问:MySql怎么查询重复的数据
  1. 答:SELECT
    * , SUM(num) as '总数'
    FROM
    td_text
    WHERE
    NAME IN ( SELECT NAME FROM td_text GROUP BY NAME HAVING count(NAME) > 1 )
    GROUP BY `name`
    td_text 表数据:
    执行sql后
  2. 答:select name,sum(num) from table group by name
    --group by 分组查询可以实现,根据名称分组查询累加数量
  3. 答:select sum(数量),名称 from table group by 名称;
    你按照这个写一下就可以了
  4. 答:select name, sum(number) from table group by name ;
  5. 答:select name, sum(count) as total from table group by name
问:mysql查询指定字段值下重复数据方法
  1. 答:select
    *
    from
    credit
    where
    project_id
    =160990
    and
    PERSON_ID
    in
    (
    select
    PERSON_ID
    from
    (select
    PERSON_ID
    ,
    count(1)
    from
    credit
    where
    project_id
    =160990
    group
    by
    PERSON_ID
    having
    count(1)>1)
    k)
问:mysql查询某字段相同的记录
  1. 答:select * from 表名 where match_id in (select match_id from 表名 where team_id='a' or team_id='b' group by match_id)
  2. 答:感觉你的描述有点乱,总结一下
    已知比赛双方队伍id,要查询比赛信息。
    按照你给的表结构,猜测,应该有三个表
    比赛记录表(match_record),比赛基本信息表(match),参赛队伍表(team),你给出了match_record表,现在已知比赛队伍id,要查询比赛信息。
    select * from match_record a,match b
    where aid
    and a查询具有相同数据的记录
    select  * from mytable where numA in(select numB from mytable);
    2id < p2id小于p2name = p2email = p2id < p2.id;
    如果需求是只要把重复的删掉,保留最新的就行可以使用上面的语句。
    如果需要稍微复杂,可以用group by,count精准控制重复n次的情况。
  3. 答:其实你会用英文搜索的话。可以很方便在stack overflow上 找到相关的信息 真的学CS的就不要用百度了 用google你会发现一个不一样的世界的
    随便贴一个
    sql - How can I remove duplicate rows?
    稍微讲一下其中一个思路(里面有很多很好的答案 你可以自己去看)
    就是做一个group by 保留其中id 最大的(你说自增长 id最大的应该就是最新的)就可以了
    具体sql query 可以这样写
    delete from test where id not in(
    select name,email,max(id) from test
    group by name,email having id is not null)
  4. 答:其实你会用英文搜索的话。可以很方便在stack overflow上 找到相关的信息 真的学CS的就不要用百度了 用google你会发现一个不一样的世界的
    随便贴一个
    sql - How can I remove duplicate rows?
    稍微讲一下其中一个思路(里面有很多很好的答案 你可以自己去看)
    就是做一个group by 保留其中id 最大的(你说自增长 id最大的应该就是最新的)就可以了
    具体sql query 可以这样写
    delete from test where id not in(
    select name,email,max(id) from test
    group by name,email having id is not null)
  5. 答:假设表名为test:
    select * from (select *, concat(name,code) as __f from test order by date desc) __t group by __f;
  6. 答:在查询视图直接写查询语句:
    use 数据库名
    go
    select distinct 字段  from (select 字段1 as a from 表名 union select 字段2 as a from 表名) as 查询1
  7. 答:用group by 就可以,对字段进行分组,数据相同的只会出来一条
点击进入下载PDF全文

相关文章

QQ咨询