MySQL中update...inner join...


2017-02-25

      现有count_table、alarm_table两张表,分别如下:

      

     

  需求:依据cust_id分组查询alarm_table表中各个cust_id今天的告警次数count(*),用count(*)更新count_table表对应cust_id的alarm_count数据

      SQL语句如下:

  update

    count_table c inner join

     (select count(*) cout,cust_id from alarm_table

        where

          to_days(alarm_date) = to_days(now())

        group by cust_id) z
    on c.cust_id = z.cust_id

  set

    c.alarm_count=z.cout,c.date=current_date
  where

    c.cust_id = z.cust_id;

  结果如下:

  

  

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM