MySQL 查询某个字段不重复的所有记录_MySQL
Jun 01, 2016 pm 01:19 PM
bitsCN.com
假设现在有如下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只能作用于一个字段
想请教应该怎么写
答案:
select a.* from book a right join (
select max(id) id from book group by title) b on b.id = a.id
where a.id is not null
如果选第一条符合的记录,那么用min(id)就行了
select a.* from book a right join (
select min(id) id from book group by title) b on b.id = a.id
where a.id is not null
bitsCN.com

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products?

How to view and manage Linux command history

How to check call history in iPhone and export it?

C# Development Advice: Logging and Monitoring Systems

How to log and monitor Java development projects

How to view your medication log history in the Health app on iPhone
