SQL group by to remove duplicates and sort by other fields

SQL group by to remove duplicates and sort by other fields

need:

Merge identical items of one field and sort them by another time field.

example:

At the beginning

select city from table group by city order by date desc

An error will be reported because date is not included in the aggregate function or GROUP BY clause

Then put the date into the group by:

select city from table group by city,date order by date desc

Get the result

But the results are still repeated and there is no solution

If it is not sorted by time, it will affect my subsequent operations, so I searched Baidu for a long time and finally found a solution:

Correct way to write:

select city from table group by city order by max(date) desc

Found a very magical result

Then I looked for some more information and found the magic of max():

select city,max(date) as d1 from table group by city,d1 order by d1 desc

Write it here in front and you can see the time sorting

If there is a better way, please share with us.

Supplement: Deduplication in MYSQL, the difference between DISTINCT and GROUP BY

For example, there is the following table user:

Distinct will filter out records that have duplicate fields after it.

Use distinct to return unique user names: select distinct name from user;, the result is:

Use distinct to return unique names and ids: select distinct name,id from user;, the result is:

If you write distinct name,id like this, MySQL will think that you want to filter out records with duplicates in both the name and id fields.

If the sql is written like this:

select id,distinct name from user

In this case, MySQL will report an error because distinct must be placed at the beginning of the field to be queried.

Group by can remove duplicates from one of the multiple fields to be queried:

select id,name from user group by name;

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Detailed explanation of the usage of Group By in SQL
  • SQL removes duplicate records (seven types)
  • In-depth analysis of the use of group by and having in SQL
  • Sorting by multiple fields and problem solving in MySQL

<<:  Similar to HTML tags: strong and em, q, cite, blockquote

>>:  The simplest form implementation of Flexbox layout

Recommend

How to write a Node.JS version of a game

Table of contents Overview Build Process Related ...

Summary of pitfalls in importing ova files into vmware

Source of the problem As we all know, all network...

Tkinter uses js canvas to achieve gradient color

Table of contents 1. Use RGB to represent color 2...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

How to implement Linux automatic shutdown when the battery is low

Preface The electricity in my residence has been ...

Do you know how to use the flash wmode attribute in web pages?

When doing web development, you may encounter the...

Vue implements an Input component that gets the key display shortcut key effect

I encountered a requirement to customize shortcut...

Detailed explanation of the usage of 5 different values ​​of CSS position

The position property The position property speci...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...

Vue Page Stack Manager Details

Table of contents 2. Tried methods 2.1 keep-alive...

Detailed explanation of MySQL file storage

What is a file system We know that storage engine...

SQL left join and right join principle and example analysis

There are two tables, and the records in table A ...

Will this SQL writing method really cause the index to fail?

Preface There are often some articles on the Inte...