Sorts a field in a database table by deduplication.
1. Problem background
In a database, t_tab_ AB has two fields, a and B, as shown below:
Before query: After query:
A B
1 2 1 2
1 3 2 3
1 4 3 4
1 5
2 3
2 4
2 5
3 4
3 5
2. Solution
Groups and sorts a by querying the minimum values in a and B.
Select t. a, min (t. B) from t_tab_ AB group by t. a order by t.
3. Implementation result
A B
1 2
2 3
3 4
SQL groups the query results of a table according to a field, sorts the results by another field, and then sets an auto-increment field for each group of data.
Select * from table group by row1 order by row2
Set the auto-increment field again. This should be implemented through the trigger during data input.
I want to query all the data in a table in the database and sort it by group and order.
Select
Group column 1, group column 2 ...,
Aggregate column 1, aggregate Column 2...
From table
Group by group column 1, group column 2...
Order by group column or aggregate Column
--------
In a group query, the column displayed as the query result must be a group column or an aggregate column.
For example, sum (), count (), avg (), and so on.