表A                               
c_id     name                        
1        a
2        b表B
id    c_id     title
1     1        天上人间来的
2     1        香格里拉来的
3     2        故宫来的
4     2        秦陵来的联合查询,请问怎样查询去掉去表B中c_id重复的记录呢,B表中只查询出一条匹配最新(根据id倒序即可)记录。
显示结果要如下:
c_id     name           title
1         a             香格里拉来的
2         b             秦陵来的注:不用group by 

解决方案 »

  1.   

    select *
    from B b1
    where not exists (select 1 from B b2 where b1.c_id=b2.c_id and b1.id<b2.id)
      

  2.   

    select * from B t
    where not exists(select 1 from A b where t.c_id=b.c_id and t.id<b.id);
      

  3.   

    mysql> select * from a;
    +------+------+
    | c_id | name |
    +------+------+
    |    1 | a    | 
    |    2 | b    | 
    +------+------+
    2 rows in set (0.03 sec)mysql> select * from b;
    +------+------+-------+
    | id   | c_id | title |
    +------+------+-------+
    |    1 |    1 | a1    | 
    |    2 |    1 | a2    | 
    |    3 |    2 | a3    | 
    |    4 |    2 | a4    | 
    +------+------+-------+
    4 rows in set (0.02 sec)mysql> select t.c_id,a.name,t.title from a join (select b1.* from b join b b1 where b.c_id=b1.c_id and b.id < b1.id) t where t.c_id=a.c_id;
    +------+------+-------+
    | c_id | name | title |
    +------+------+-------+
    |    1 | a    | a2    | 
    |    2 | b    | a4    | 
    +------+------+-------+
    2 rows in set (0.04 sec)
      

  4.   

    参考下贴中的多种方法http://blog.csdn.net/acmain_chm/article/details/4126306
    [征集]分组取最大N条记录方法征集,及散分....