这是我的情况:我有一个表,其中重复的值有效(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有不同的TIME)。
这是表结构
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。
答案 0 :(得分:2)
尝试:
SELECT DISTINCT SATELLITE_ID, ATT_TYPE_ID, TIME, ROLL, PITCH, YAW
FROM ATTITUDE
答案 1 :(得分:2)
您可以将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
结果:
| ID | SATELLITE_ID | ATT_TYPE_ID | TIME | ROLL | PITCH | YAW |
---------------------------------------------------------------
| 2 | 1 | 1 | 2012 | 1 | 2 | 1 |
| 3 | 1 | 1 | 2011 | 1 | 2 | 1 |
答案 2 :(得分: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
答案 3 :(得分: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的不同值任意分组。如果你遗漏了其中的一些字段,那只意味着可能会有更少的群体,彼此之间的差异较小。