id fid title date
1 14 标题1 07-01
2 14 标题2 07-02
3 15 标题3 07-03
4 16 标题4 07-04
如上表,我想将fid相同的去掉重复,取最后一次添加的,也就是id最大的值。最后结果能得到下表的情况。
id fid title date
2 14 标题2 07-02
3 15 标题3 07-03
4 16 标题4 07-04
select *
from table ###
where not exists (
select * from table ###
where # = #
and ## < ##
)
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是 distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,只有用二重循环查询来解决。
给个例子把,比如:表table_a 4条数据
id A B C D
01 ab 1a2 1b2 121
02 ab 2a3 3b3 4a1
03 ac 1a2 1b2 121
04 ac 2a4 3b2 52g
何让A字段重复取条 比
01 ab 1a2 1b2 121
03 ac 1a2 1b2 121
保留相同A值id行
select *
from table_a a
where not exists (
select 1 from table_a b
where b.A = a.A
and b.id < a.id
)
不对,fid还是重复出现的
追答select a.* from tb a,(select max(id) as id,fid from tb group by fid) b
where a.id=b.id and a.fid=b.fid
成...功了。。。能稍微讲解下a后面加点,b后面什么不加是什么 意思吗??
追答什么意思啊?哪个点?
追问a.* 和 b where
追答a.*表示查询表中所有的字段,a 是表tb 的别名
本回答被提问者采纳