56,681
社区成员
发帖
与我相关
我的任务
分享
mysql> update z_b set lastposttime=(select max(posttime) from z_a where z_b.tid
= z_a.tid),lastposter=(select poster from z_a where z_b.tid= z_a.tid order by po
sttime desc limit 0,1),replies=(select count(*) from z_a where z_b.tid= z_a.tid
group by tid);
update z_b as d inner join (select a.tid,a.pit,a.posttime,a.poster, b.cnt from z_a as a ,(select max(pit) as pit,count(*) as cnt from z_a where first=0 group by tid) as b where a.pit=b.pit) as c on(d.tid=c.tid) set lastposter=c.poster,lastposttime=c.posttime,replies=c.cnt
update z_b
set lastposttime=(Select max(posttime) From z_a where tid=z_b.tid),
lastposter=(Select poster from z_a where tid=z_b.tid and posttime=(select max(posttime) from z_a where tid=z_b.tid)),
replies=(Select count(*) from z_a where tid=z_b.tid);
mysql> select * from z_a;
+-----+------+-------+------------+---------+--------+
| pit | tid | first | posttime | message | poster |
+-----+------+-------+------------+---------+--------+
| 1 | 1 | 1 | 1280000000 | x | user1 |
| 2 | 1 | 0 | 1280000001 | x | user2 |
| 3 | 1 | 0 | 1280000002 | x | user3 |
| 4 | 1 | 0 | 1280000003 | x | user4 |
| 5 | 1 | 0 | 1280000004 | x | user5 |
| 6 | 1 | 0 | 1280000005 | x | user6 |
| 7 | 2 | 1 | 1280000006 | x | user7 |
| 8 | 2 | 0 | 1280000007 | x | user8 |
| 9 | 2 | 0 | 1280000008 | x | user9 |
| 10 | 2 | 0 | 1280000009 | x | user10 |
+-----+------+-------+------------+---------+--------+
10 rows in set (0.00 sec)
mysql> select * from z_b;
+-----+--------------+------------+---------+
| tid | lastposttime | lastposter | replies |
+-----+--------------+------------+---------+
| 1 | 0 | | 0 |
| 2 | 0 | | 0 |
+-----+--------------+------------+---------+
2 rows in set (0.00 sec)
mysql> update z_b
-> set lastposttime=(select max(posttime) from z_a where tid=z_b.tid),
-> lastposter=(select poster from z_a where tid=z_b.tid and posttime=(select max(posttime) from z_a where tid=z_b.tid)),
-> replies=(select count(*) from z_a where tid=z_b.tid);
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from z_b;
+-----+--------------+------------+---------+
| tid | lastposttime | lastposter | replies |
+-----+--------------+------------+---------+
| 1 | 1280000005 | user6 | 6 |
| 2 | 1280000009 | user10 | 4 |
+-----+--------------+------------+---------+
2 rows in set (0.02 sec)
mysql>
select C.tid,D.posttime,D.poster,C.num
from
(
select A.tid,count(*)-1 as num
from A,B
where A.tid = B.tid
group by A.tid
)C,(
select A1.tid,A1.posttime,A1.poster
from A A1
where not exits (select 1 from A A2 where A1.tid=A2.tid and A1.posttime<B.posttime)
)D
where C.tid = D.tid
是discuz的主贴表和回复表 将贴子的回复数量,最后回复时间,最后回复的用户 更新到贴子主表
如有 回复表A 数据
pid tid first posttime message poster
1 1 1 1280000000 x user1
2 1 0 1280000001 x user2
3 1 0 1280000002 x user3
4 1 0 1280000003 x user4
5 1 0 1280000004 x user5
6 1 0 1280000005 x user6
7 2 1 1280000006 x user7
8 2 0 1280000007 x user8
9 2 0 1280000008 x user9
10 2 0 1280000009 x user10
first=0的为回复
主贴表 数据
tid lastposttime lastposter replies(回复数量)
1 0 0 0
2 0 0 0
执行更新统计后得到以下数据
tid lastposttime lastposter replies
1 1280000005 user6 5
2 1280000009 user10 3
求这样的更新统计的 的sql语句