用户表
userid Name
1 Test1
2 Test2
3 Test3
4 Test4
书桌
userid Destination
1 US
2 UK
1 MEXICO
1 INDIA
3 CANADA
1 CHINA
4 CANADA
1 CHINA
根据上表,我应该使用什么方法在插入查询时显示此结果
这是我想要的结果
bookCount userid
5 1
2 2
1 3
1 4
答案 0 :(得分:0)
试试这个:
SELECT COUNT(*) bookCount, userid
FROM Book
GROUP BY userid;
有关见解,请参阅MySQL GROUP BY。您不需要使用联接:
SELECT book.userid, COUNT(*) AS `count`
FROM book INNER JOIN `user` ON book.userid=`user`.userid
GROUP BY book.userid
HAVING COUNT(*)>1;