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;

  结果如下:

  

  

 

 

posted @ 2017-02-25 21:00  If-Only  阅读(17122)  评论(0编辑  收藏  举报