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

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

Jun 01, 2016 pm 01:19 PM
title Record

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

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 view and manage Linux command history Aug 01, 2023 pm 09:17 PM

How to view and manage Linux command history

How to check call history in iPhone and export it? How to check call history in iPhone and export it? Jul 05, 2023 pm 12:54 PM

How to check call history in iPhone and export it?

C# Development Advice: Logging and Monitoring Systems C# Development Advice: Logging and Monitoring Systems Nov 22, 2023 pm 08:30 PM

C# Development Advice: Logging and Monitoring Systems

How to log and monitor Java development projects How to log and monitor Java development projects Nov 03, 2023 am 10:09 AM

How to log and monitor Java development projects

what does title mean what does title mean Aug 04, 2023 am 11:18 AM

what does title mean

How to view your medication log history in the Health app on iPhone How to view your medication log history in the Health app on iPhone Nov 29, 2023 pm 08:46 PM

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

What is the meaning of title in HTML What is the meaning of title in HTML Mar 06, 2024 am 09:53 AM

What is the meaning of title in HTML

See all articles