MySQL 查询所有title不重复的记录

很文博客hinven.com 数据库评论63字数 447阅读模式
广告也精彩

现在想从这5条记录中查询所有title不重复的记录
假设现在有如下N条记录 表明叫book
id author title
1 aaa AAA
2 bbb BBB
3 ccc CCC
4 ddd DDD
5 eee AAA
现在想从这5条记录中查询所有title不重复的记录
select distinct title,author from book这样是不可以的 因为distinct只能作用于一个字段
想请教应该怎么写
答案:

  1. select a.* from book a right join (
  2. select max(id) id from book group by title) b on b.id = a.id
  3. where a.id is not null

如果选第一条符合的记录,那么用min(id)就行了文章源自很文博客https://www.hinven.com很文博客-https://www.hinven.com/53022.html

  1. select a.* from book a right join (
  2. select min(id) id from book group by title) b on b.id = a.id
  3. where a.id is not null

 文章源自很文博客https://www.hinven.com很文博客-https://www.hinven.com/53022.html

文章源自很文博客https://www.hinven.com很文博客-https://www.hinven.com/53022.html

工具:作品在线观看

女优:最新作品观看

中文:国语在线观看

weinxin
我的微信
扫一扫更精彩
大家的支持是我更新的动力!!!
 
广告也精彩
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证