A表:A_id  A_Name
     1      adaad
     2      sdfsdfB表:B_id  A_id  B_Name    B_date
      1     1     sfds      2006-01-01
      2     1     sfsds     2006-01-02
      3     1     rwe       2006-01-03
      4     2     ewr       2006-01-01
A表中的A_id和B表种的A_id是一对多的关系
需要列出
A表的A_id的字段值在B表中A_id的字段值相同的记录按时间最晚的一条记录排列出来
这个select语句怎么写?就是类似论坛发贴,有新回帖时,该帖子会被列在帖子列表的最上方

解决方案 »

  1.   

    try:select a.a_name,b.b_date from tableA a inner join 
    (select a_id, max(b_date) b_date from tableB group by a_id) b
    on a.a_id=b.a_id;
      

  2.   


    select * 
    from tableA a 
    inner join 
    (select a_id,max(b_date) b_date from tableB group by a_id) b 
    on a.a_id=b.a_id 
    inner join tableB on b.a_id = tableB.a_id and b.b_data = tableB.B_date
      

  3.   


    试试这个select * from tableA ,
    (select a_id,max(b_date) b_date from tableB group by a_id) b ,tableB
    where b.a_id = tableB.a_id and b.b_data = tableB.B_date and a.a_id=b.a_id ;另外,mysql 4.1 之后才支持子查询
    你最好先看看你的mysql是否支持