简体   繁体   English

MySQL:选择除列之外仅具有唯一值的行

[英]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. 这是我的情况:我有一个表,其中重复的值是有效的(ID字段除外),并且我只想检索唯一的值。

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). 我只想检索2和3(ID 1和2是“相等”,而3具有不同的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: 您可以将max()集合应用于ID列,然后将其余部分应用于GROUP BY

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 参见带有演示的SQL Fiddle

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 或者你可以使用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. 这可以根据SATELLITE_ID,ATT_TYPE_ID,TIME,ROLL,PITCH和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. 如果您忽略了其中一些字段,那仅意味着组的数量可能会减少,彼此之间差异的原因也就更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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