表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
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让结果竖着显示
- mysql按小时统计注册人数怎么弄,只有一个注册时间字段格式为('2010-06-10 20:39:18')
- 求一mysql语句,题目如下:(补充上午的)
- 更新语句错误,到底为啥呀 ,各位帮忙看看!
- MYSQL 插入问题 在线等
- grant错误,解释得详细的加分!
- 怎样修复mysql表---Incorrect information in file: '.\test\stuff.frm'
- 求助:vc+ado + mysql+myodbc --〉timestamp(or datetime)
- MySql 内部游标的循环嵌套使用
- AES_ENCRYPT不能赋值给变量
- insert...select问题
- mysql添加索引时会不会加锁
from B b1
where not exists (select 1 from B b2 where b1.c_id=b2.c_id and b1.id<b2.id)
where not exists(select 1 from A b where t.c_id=b.c_id and t.id<b.id);
+------+------+
| 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)
[征集]分组取最大N条记录方法征集,及散分....