日期:2014-05-16  浏览次数:20860 次

关于论坛的列表sql
表结构如下

表名:test
id:主题id
parentid:如果是主题就为0   ,如果是回复,就为主题id
title:标题
uname:用户名

如果能显示下面的效果怎样构造这个sql呢,主要是最后回复有点麻烦

标题                 作者                 最后回复(这里显示最后回复的用户名)

------解决方案--------------------
mysql> create table bb
-> (
-> id int not null auto_increment primary key,
-> parentid char(1) default 1,
-> title varchar(500),
-> uname varchar(64)
-> );
Query OK, 0 rows affected (0.13 sec)

mysql> insert into bb values(null,0, 'I love test ', 'csdn1 ');
Query OK, 1 row affected (0.06 sec)

mysql> insert into bb values(null,0, 'I love test2 ', 'csdn2 '),(null,1, 'I love test
1 ', 'csdn3 ');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from bb;
+----+----------+--------------+-------+
| id | parentid | title | uname |
+----+----------+--------------+-------+
| 1 | 0 | I love test | csdn1 |
| 2 | 0 | I love test2 | csdn2 |
| 3 | 1 | I love test1 | csdn3 |
+----+----------+--------------+-------+
3 rows in set (0.02 sec)
mysql> select * from bb where parentid=1 order by id desc limit 1;
+----+----------+--------------+-------+
| id | parentid | title | uname |
+----+----------+--------------+-------+
| 3 | 1 | I love test1 | csdn3 |
+----+----------+--------------+-------+
1 row in set (0.00 sec)