为什么没人回答,是不是我说的不清楚,我换一种方式再说一遍:
表:
table(id,title,temp) se(id,title,temp)
se的temp关联到table的id;
table 到 se 的关联是 0个或n个;
要搜索内容
table 的每一纪录,关联的se的纪录数,关联的se的纪录最新的一条(id最大)数据库是mysql的,不能用视图和嵌套查询.
表:
table(id,title,temp) se(id,title,temp)
se的temp关联到table的id;
table 到 se 的关联是 0个或n个;
要搜索内容
table 的每一纪录,关联的se的纪录数,关联的se的纪录最新的一条(id最大)数据库是mysql的,不能用视图和嵌套查询.
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=78128
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=6420
from table
where temp=0 and temp=id
group by id
order by id desc 试试看 这样行不行
得到的数据是安id分组的,不是我要的,我要的是按temp组
select tablemain.title as maintitle,tabletemp.title as temptitle,
count(tabletemp.id) as tempnum
where tablemain.temp='0' and tabletemp.temp=tablemain.id
group by tablemain.id
order by tablemain.id desc,tabletemp.id desc;
谢谢你的回复,你的sql我试了,和我以开始的效果一样,只能得到tabletemp每一组(一个tablemain的纪录对应一组tabletemp的纪录)的第一条。我只想得到每一组的最后一条,写法如你的sql:
select tablemain.title as maintitle,tabletemp.title as temptitle,count(tabletemp.id) as tempnum
where tablemain.temp='0' and tabletemp.temp=tablemain.id
group by tablemain.id
order by tablemain.id desc,tabletemp.id desc;tabletemp.title 应该是该组(和同一个tablemain的纪录关联)中最后一条(最新插入的一条)的title
caeate temporary table test select * from tab_name where temp=0
select * from test和 和该纪录相关联的(temp=id的)纪录数
select * from tbl_name, test where tbl_name.???=test.??? #关联字段?和最新的一条纪录(id最大)
...
select *
如果不能嵌套,你可以分开啊,大不了建临时视图?
主题和主题的回复在一个表里面,
back字段为0则为主题,不为零则为回复,并且是所属主题的id
这样,显示主题的列表时,显示该主题的回复次数,最新回复的名称。