zoukankan      html  css  js  c++  java
  • mysql 清除多列重复数据,只保留其中一条

    -- 创建临时表存储查询出的数据
    drop temporary table if exists childtable;
    create temporary table childtable (select * from tb_child a where 
    (a.patriarch_id,a.student_id) in (select patriarch_id,student_id from tb_child group by patriarch_id,student_id 
    having count(*) > 1)
    and child_id not in (select min(child_id) from tb_child group 
    by patriarch_id,student_id having count(*)>1));
    
    select *From childtable
    drop temporary table childtable;
    -- 根据条件删除临时表格中的数据
    delete from tb_child  where  child_id in (select childtable.child_id from childtable)
    
    --查询出 绑定关系表中 家长id和学生id重复的数据。但是不包含 绑定关系id最小的那一条记录
    select * from tb_child a where 
    (a.patriarch_id,a.student_id) in (select patriarch_id,student_id from tb_child group by patriarch_id,student_id 
    having count(*) > 1)
    and child_id not in (select min(child_id) from tb_child group 
    by patriarch_id,student_id having count(*)>1)
    
    -- 查询出具有patriarch_id ,student_id 重复的数据(排除id最小的第一条数据) 带行号
    select (@rownum := @rownum + 1) AS rownum ,a.* from tb_child a, (SELECT  @rownum := 0) AS rn  where 
    (a.patriarch_id,a.student_id) in (select patriarch_id,student_id from tb_child group by patriarch_id,student_id 
    having count(*) > 1)
    and child_id not in (select min(child_id) from tb_child group 
    by patriarch_id,student_id having count(*)>1)
    
    --查询出具有patriarch_id ,student_id 重复的数据 带行号
    select (@rownum := @rownum + 1) AS rownum ,ci.* from tb_child ci, (SELECT 
        @rownum := 0) AS rn  where patriarch_id in (select patriarch_id from tb_child group by patriarch_id,student_id having count(*) > 1) 
  • 相关阅读:
    awk书上练习
    矩阵运算
    从最大似然到EM算法浅解
    numpy 练习
    python lxml教程
    pycharm快捷键
    python正则表达式教程
    三门问题
    Solr本地服务器搭建及查询
    git简单使用
  • 原文地址:https://www.cnblogs.com/chongyao/p/12667942.html
Copyright © 2011-2022 走看看