mysql 如何统计字段里面元素出现的次数

2025-03-24 09:49:19
推荐回答(2个)
回答1:

一、你的意思是这个意思吗:
select count(*) from 表 where 用户id = ? and 商品id = ?;

二、还是根据用户id查询出用户对哪些商品发生了行为,然后再统计对每一个商品的行为:
1、统计用户发生行为的商品集合:
select * from 表 where 用户id = ?;
2、遍历商品集合,然后统计用户对每个商品的行为次数:
select count(*) from 表 where 用户id = ? and 商品id = ?;

回答2:

SELECT d_cname,count(*)
FROM table_diancai
group BY d_cname