Home > Database > Mysql Tutorial > MySQL 查询某个字段不重复的所有记录

MySQL 查询某个字段不重复的所有记录

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 18:00:47
Original
1119 people have browsed it

现在想从这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只能作用于一个字段
想请教应该怎么写
答案:
代码如下:
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
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template