为什么没人回答,是不是我说的不清楚,我换一种方式再说一遍:
表:
table(id,title,temp)   se(id,title,temp) 
se的temp关联到table的id;
table 到 se 的关联是 0个或n个;
要搜索内容
table 的每一纪录,关联的se的纪录数,关联的se的纪录最新的一条(id最大)数据库是mysql的,不能用视图和嵌套查询.

解决方案 »

  1.   

    select max(id),count(id),temp from se group by temp
      

  2.   

    http://community.csdn.net/Expert/topic/3073/3073195.xml?temp=.1714899参照这个,问题和你差不多, 但是这个是在oracle中的
      

  3.   

    不行的,mysql是不支持嵌套的。
      

  4.   

    仔细地看一下这两个帖吧.
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=78128
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=6420
      

  5.   

    select table.title as maintitle , temp as temptitle , count(id) as tempnum
    from table 
    where temp=0 and temp=id
    group by id
    order by id desc 试试看 这样行不行
      

  6.   

    这个我早时过了
    得到的数据是安id分组的,不是我要的,我要的是按temp组
      

  7.   

    我估计你写错了,注意一下FROM子句.我写了一个,起码可以执行。
    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;
      

  8.   

    to:neilqi(neil) 
    谢谢你的回复,你的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
      

  9.   

    sql 好像是先分组然后再排序的,所以我想不出办法来得到一组纪录的最后一条,之能得到第一条
      

  10.   

    要分几次做,mysql嘛我想得到table每一个temp=0的纪录,
    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 *
      

  11.   

    现在我是分开做的,,我想如果能以此搞定,效率会高一些,,,,谢谢你,xuzuning
      

  12.   

    没有用过my sql 只用过oracle
    如果不能嵌套,你可以分开啊,大不了建临时视图?
      

  13.   

    本来是这样的,一个复杂的留言板(简易论坛)
    主题和主题的回复在一个表里面,
    back字段为0则为主题,不为零则为回复,并且是所属主题的id
    这样,显示主题的列表时,显示该主题的回复次数,最新回复的名称。