简体   繁体   中英

MySQL: Select rows that have only unique values except for a column

Here is my situation: I have a table which duplicated values are valid (except by the ID field), and I'd like to retrieve only unique values.

For instance, if I have the registers:

+----+--------------+-------------+------+------+-------+-----+
| ID | SATELLITE_ID | ATT_TYPE_ID | TIME | ROLL | PITCH | YAW |
+----+--------------+-------------+------+------+-------+-----+
|  1 |            1 |           1 | 2012 |  1.0 |   2.0 | 1.3 |
+----+--------------+-------------+------+------+-------+-----+
|  2 |            1 |           1 | 2012 |  1.0 |   2.0 | 1.3 |
+----+--------------+-------------+------+------+-------+-----+
|  3 |            1 |           1 | 2011 |  1.0 |   2.0 | 1.3 |
+----+--------------+-------------+------+------+-------+-----+

I'd like to retrieve just 2 and 3 (ID 1 and 2 are "equal", and 3 has different TIME).

Here it is the table structure

mysql> describe attitude;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| ID           | int(11)      | NO   | PRI | NULL    | auto_increment |
| SATELLITE_ID | int(11)      | NO   |     | NULL    |                |
| ATT_TYPE_ID  | int(11)      | NO   |     | NULL    |                |
| TIME         | varchar(4)   | NO   |     | NULL    |                |
| ROLL         | double       | NO   |     | NULL    |                |
| PITCH        | double       | NO   |     | NULL    |                |
| YAW          | double       | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

Thx.

Try:

SELECT DISTINCT SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW
FROM ATTITUDE

You can apply the max() aggregate to the ID column and then GROUP BY the rest:

select max(id) id, SATELLITE_ID, ATT_TYPE_ID, TIME, Roll, Pitch, yaw
from attitude
group by SATELLITE_ID, ATT_TYPE_ID, TIME, Roll, Pitch, yaw
order by id

See SQL Fiddle with Demo

Result:

| ID | SATELLITE_ID | ATT_TYPE_ID | TIME | ROLL | PITCH | YAW |
---------------------------------------------------------------
|  2 |            1 |           1 | 2012 |    1 |     2 |   1 |
|  3 |            1 |           1 | 2011 |    1 |     2 |   1 |

Try this:

select SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW 
from ATTITUDE
group by SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW 

or you can use distinct

select distinct SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW 
from ATTITUDE
select max(ID) from attitude
  group by SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW;

This arbitrarily groups by differing values of SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, and YAW. If you left out some of these fields, that only means there will potentially be less groups, having fewer reasons to differ from one another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM