2012-12-19 82 views
1

我發現最接近的事我所尋找的是,所以我會用它作爲鑑於此數據集MySQL的 - 只選擇行,其中列有獨特的價值

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

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

我想只返回

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

,因爲它是唯一的獨特行

這是可能與MySQL?

+0

注意,在大多數情況下,它是一個非常糟糕的主意,有重複在表格中。你最好添加一個字段「OCCURENCES」。 –

+0

@AntoinePinsard。 。 。它們不是重複的。 ID是不同的。 –

+0

@GordonLinoff我的意思是排除ID。除非這些行根本不代表同一個對象,並且合法擁有它們自己的ID(例如用於外鍵)。 –

回答

4
SELECT 
    ID, 
    SATTELLITE_ID, 
    ATT_TYPE_ID, 
    TIME, 
    ROLL, 
    PITCH, 
    YAW 
FROM Table 
GROUP BY 
    SATTELLITE_ID, 
    ATT_TYPE_ID, 
    TIME, 
    ROLL, 
    PITCH, 
    YAW 
HAVING COUNT(*) = 1 
0

使用group byhaving count(*) = 1

1

試試這個:

SELECT * FROM temptbl 
GROUP BY satellite_id, att_type_id, time, roll, pitch, yaw 
HAVING COUNT(*) = 1;