1

这是我的情况:我有一个重复值有效的表(ID 字段除外),我只想检索唯一值。

例如,如果我有寄存器:

+----+--------------+-------------+------+------+-------+-----+
| 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 |
+----+--------------+-------------+------+------+-------+-----+

我只想检索 2 和 3(ID 1 和 2 是“相等的”,而 3 的时间不同)。

这是表结构

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    |                |
+--------------+--------------+------+-----+---------+----------------+

谢谢。

4

4 回答 4

3

您可以将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

请参阅带有演示的 SQL Fiddle

结果:

| ID | SATELLITE_ID | ATT_TYPE_ID | TIME | ROLL | PITCH | YAW |
---------------------------------------------------------------
|  2 |            1 |           1 | 2012 |    1 |     2 |   1 |
|  3 |            1 |           1 | 2011 |    1 |     2 |   1 |
于 2012-11-28T14:07:21.687 回答
2

尝试:

SELECT DISTINCT SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW
FROM ATTITUDE
于 2012-11-28T14:07:09.237 回答
1

尝试这个:

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

或者你可以使用distinct

select distinct SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW 
from ATTITUDE
于 2012-11-28T14:06:35.057 回答
0
select max(ID) from attitude
  group by SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW;

这通过 SATELLITE_ID、ATT_TYPE_ID、TIME、ROLL、PITCH 和 YAW 的不同值任意分组。如果您遗漏了其中一些字段,那仅意味着可能会有更少的群体,更少的理由彼此不同。

于 2012-11-28T14:04:20.257 回答