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;
結果如下: