2012-04-23 82 views
1

關於MySQL的集團通過

TableName 
============================== 
ID | A_ID | BLABLA | TIME 
----------------------------------------------- 
1 | n1 | X | Time 
2 | n1 | X | Time 
3 | n1 | X | Time 
4 | n1 | Y | Time 
5 | n1 | Z | Time 
6 | n2 | Y | Time 
7 | n2 | Y | Time 
8 | n2 | Y | Time 
9 | n3 | X | Time 
10 | n4 | Z | Time 
11 | n4 | Z | Time 
13 | n5 | | Time 
14 | n6 | X | Time 
15 | n7 | | Time 
16 | n8 | | Time 
================================== 

我要爲節目表查詢的結果,只使用MySQL查詢。

============================== 
A_ID | BLABLA 
----------------------------------------------- 
n1 | X 
n1 | Y 
n1 | Z 
n2 | Y 
n3 | X 
n4 | Z 
n5 | 
n6 | X 
n7 | 
n8 | 
================================== 
+1

所以,如果你有'n1'其中有'BLABLA'值'X','Y',和'Z',你如何決定你想要哪些?最早的那個?最小的ID? – 2012-04-23 04:42:26

回答

4

所有你所需要的僅僅是GROUP BY A_ID, BLABLA,像這樣:

SELECT A_ID, BLABLA 
FROM TableName 
GROUP BY A_ID, BLABLA 
+0

非常感謝。 – aCmySQL 2012-04-23 05:14:22

+2

@ user825919,歡迎您,如果您覺得有用,請接受答案 – 2012-04-23 05:15:43