1

我正在 mysql 事件中进行选择,然后尝试使用来自该选择的数据更新另一个表。但它似乎不起作用。

我正在做:

BEGIN
select read_pub_id, read_artc_id, count(read_artc_id) as times_read from reads_t GROUP BY read_pub_id, read_artc_id;
update reads_totals set read_total = times_read where pub_id = read_pub_id and artc_id = read_artc_id;
update reads_totals set ts=now(); /*This is for testing*/
END

只有 ts 得到更新。这意味着该事件正在运行。

这里是否要添加一个while循环,如何添加?这样做的正确方法是什么?

我之前与此相关的问题在这里:

https://stackoverflow.com/questions/13110393/doing-a-count-along-with-distinct-to-check-number-of-times-a-post-was-read

4

2 回答 2

1

你的意思:

UPDATE reads_totals T
SET read_total = (SELECT count(read_artc_id) 
                  FROM reads_t R 
                  WHERE R.read_pub_id = T.pub_id 
                  AND R.read_artc_id = T.artc_id);

此查询虽然会更新中的每一行reads_totals但我不确定这是否真的是您想要的。

于 2012-10-29T04:51:33.327 回答
1
 select read_pub_id, read_artc_id, count(read_artc_id) as times_read from reads_t 
 GROUP BY read_pub_id, read_artc_id;
 update reads_totals
 set read_total = (select count(read_artc_id) from reads_t rt 
 where pub_id = rt.read_pub_id 
 and artc_id = rt.read_artc_id);
于 2012-10-29T04:52:29.753 回答