求更新统计的sql语句

daodaoyu222 2010-11-07 04:34:27
是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语句


...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cfd406635982 2010-11-08
  • 打赏
  • 举报
回复
跟着楼上学习一下。

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);

zmjsg 2010-11-08
  • 打赏
  • 举报
回复
呵呵。。。


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 



不知道与6楼比哪个性能好点?
ACMAIN_CHM 2010-11-07
  • 打赏
  • 举报
回复
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);
ACMAIN_CHM 2010-11-07
  • 打赏
  • 举报
回复
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>
daodaoyu222 2010-11-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acmain_chm 的回复:]
最好你能直接给出create table / insert into 语句,这样别人可以直接在机器上测试并找到你需要的方案。
[/Quote] 说得不错,我以后一定注意了。

CREATE TABLE `z_a` (
`pit` int(10) unsigned NOT NULL auto_increment,
`tid` int(10) default NULL,
`first` int(10) default NULL,
`posttime` int(10) default NULL,
`message` varchar(255) default NULL,
`poster` varchar(255) default NULL,
PRIMARY KEY (`pit`),
KEY `a` (`first`),
KEY `b` (`posttime`),
KEY `c` (`message`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;



insert into `z_a`(`pit`,`tid`,`first`,`posttime`,`message`,`poster`) values (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');





CREATE TABLE `z_b` (
`tid` int(10) unsigned NOT NULL auto_increment,
`lastposttime` int(10) default NULL,
`lastposter` varchar(255) default NULL,
`replies` int(10) default NULL,
PRIMARY KEY (`tid`),
KEY `b` (`lastposttime`),
KEY `c` (`lastposter`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;



insert into `z_b`(`tid`,`lastposttime`,`lastposter`,`replies`) values (1,0,'',0),(2,0,'',0);
rucypli 2010-11-07
  • 打赏
  • 举报
回复
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
ACMAIN_CHM 2010-11-07
  • 打赏
  • 举报
回复
最好你能直接给出create table / insert into 语句,这样别人可以直接在机器上测试并找到你需要的方案。
daodaoyu222 2010-11-07
  • 打赏
  • 举报
回复
上面的不太好看清啊,我再发一下看看这样能不能看得清楚些。



是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语句






56,681

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧