使用mySQLi查询选择哪个用户拥有更多产品

时间:2019-03-01 12:22:35

标签: mysql mysqli

我有一张桌子products

id    user    products
1     user1   product
2     user2   product
3     user1   product
4     user3   product
5     user4   product

现在,我想了解哪个用户拥有最多的产品(user1)

$query = 'SELECT * FROM products WHERE ...';

对mySQLi面向对象查询有任何建议吗?

1 个答案:

答案 0 :(得分:2)

您可以这样处理:

SELECT `p`.`user` FROM `products` as p GROUP BY `user` ORDER BY count(id) DESC LIMIT 1