首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

请问一个简单的SQL有关问题

2012-09-12 
请教一个简单的SQL问题表Ac_idname1a2b表Bidc_idtitle11天上人间来的21香格里拉来的32故宫来的42秦陵来的

请教一个简单的SQL问题
表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

[解决办法]

SQL code
select * from B twhere not exists(select 1 from A b where t.c_id=b.c_id and t.id<b.id);
[解决办法]
mysql> select mm.c_id ,aa.name,mm.title from (select bb.* from B bb where not ex
ists (select 1 from B where bb.c_id = c_id and bb.id<id)) mm ,A aa where mm.c_i
d = aa.c_id;

热点排行