永发信息网

在mysql数据库中如何让某个字段有重复的只取一条

答案:3  悬赏:80  手机版
解决时间 2021-11-26 03:49
  • 提问者网友:美人性情
  • 2021-11-25 14:55
在mysql数据库中如何让某个字段有重复的只取一条
最佳答案
  • 五星知识达人网友:逃夭
  • 2021-11-25 15:05
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
)
全部回答
  • 1楼网友:老鼠爱大米
  • 2021-11-25 16:31
select * from (select * from a order by id desc) as b group by fid
  • 2楼网友:琴狂剑也妄
  • 2021-11-25 15:22
select max(id) as id,fid,title,date from table group by fid,title,date追问不对,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 的别名
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯