MYSQL选择重复值

时间:2015-06-09 21:32:35

标签: php select

是否可以检查表格中是否有重复值,如果是,那么会返回结果吗? 可以说我们有下表:

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已经重读了读书no:b09 4次

可以这样做吗? 提前谢谢。

1 个答案:

答案 0 :(得分:4)

使用GROUP BY获得您想要的内容:

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