zoukankan      html  css  js  c++  java
  • mysql 根据查询条件进行update

    -- 新增comment_num 评论数字段
    alter table tb_tips add   `comment_num` int(11) NOT NULL DEFAULT '0' COMMENT '评论数';
    
    select count(1) as comment_num ,tips_id from tb_tips_comments GROUP BY tips_id ;
    
    -- 创建临时表存储 所有tips 的统计数量
    create temporary table statistical_comments(select count(1) as comment_num ,tips_id from tb_tips_comments GROUP BY tips_id);
    
    
    -- 把评论表中有的数据但是tips表没有的数据清理掉
    select tb_tips.tips_id ,statistical_comments.tips_id  from tb_tips right join statistical_comments on tb_tips.tips_id = statistical_comments.tips_id where  tb_tips.tips_id is null ;
    
    delete FROM  tb_tips_comments where tips_id in (select statistical_comments.tips_id  from tb_tips right join statistical_comments on tb_tips.tips_id = statistical_comments.tips_id where  tb_tips.tips_id is null);
    -- 把统计的评论数同步到tips表格
    update tb_tips,statistical_comments set tb_tips.comment_num = statistical_comments.comment_num where tb_tips.tips_id = statistical_comments.tips_id;
    
    -- 查看结果是否和预期一样。
    select count(1) as comment_num ,tips_id from tb_tips_comments GROUP BY tips_id ORDER BY comment_num ,tips_id;
    select comment_num,tips_id from tb_tips where comment_num >0 ORDER BY comment_num ,tips_id ;
        -- 用完释放
    drop TEMPORARY TABLE statistical_comments;
     
  • 相关阅读:
    CodeForces 55D Beautiful numbers(数位dp+数学)
    hdu 2089 不要62(数位dp入门)
    Git版本控制
    Git初始化-添加提交以及查看状态
    linux-高并发与负载均衡-lvs-3种模型推导
    Scrapy中选择器的用法
    Scrapy命令行详解
    Scrapy框架基本用法讲解
    MaxCompute教程
    Scrapy安装报错
  • 原文地址:https://www.cnblogs.com/chongyao/p/13023650.html
Copyright © 2011-2022 走看看