2015-06-09 138 views
1

是否有可能檢查重複值的表,如果是的話會返回結果? 假設我們有如下表:MYSQL選擇重複值

table: views 
------------------ 
id | user | book | 
------------------ 
1 | 1 | b01 | 
2 | 1 | b02 | 
3 | 2 | b04 | 
4 | 2 | b09 | 
5 | 2 | b09 | 
6 | 2 | b09 | 
7 | 2 | b09 | 
8 | 3 | b03 | 
9 | 3 | b06 | 
10 | 3 | b07 | 
... 

所以......

What I want to print is this part: 
------------------ 
id | user | book | 
------------------ 
4 | 2 | b09 | 
5 | 2 | b09 | 
6 | 2 | b09 | 
7 | 2 | b09 | 

在平原的話,我們會說,用戶2具有repeadet讀書沒有:B09 4次

可以這樣做嗎? 預先感謝您。

回答

4

使用GROUP BY得到你想要的東西:

SELECT user, book, COUNT(*) as cnt FROM views GROUP BY user, book HAVING cnt > 1