Mysql sql查询:使用groupBy子句显示所有行
使用groupBy子句时是否可以显示所有表行? 经验: 预期结果:按用户\u id计数后Mysql sql查询:使用groupBy子句显示所有行,mysql,sql,Mysql,Sql,使用groupBy子句时是否可以显示所有表行? 经验: 预期结果:按用户\u id计数后 id user_id gift_id count 1 1 1 3 2 1 2 3 3 1 1 3 4 2 3 2 5 2 1 2 6 3
id user_id gift_id count
1 1 1 3
2 1 2 3
3 1 1 3
4 2 3 2
5 2 1 2
6 3 4 1
试试这个
select a.*, b.user_count from tablename as a join
(select user_id, count(*) as user_count from tablename group by user_id) as b on
a.user_id= b.user_id
他不希望所有行合并
select a.*, b.user_count from tablename as a join
(select user_id, count(*) as user_count from tablename group by user_id) as b on
a.user_id= b.user_id
SELECT id
, user_id
, gift_id
, Count(user_id) over (PARTITION BY user_id) nb_user_id
FROM matable