MYSQL帮助,如何统计结果

时间:2015-12-21 19:13:33

标签: mysql

运行这个sql:

SELECT
    user_id,
    base_item
FROM
    items
WHERE
    base_item = "202"

给了我这个http://prntscr.com/9gwwxq 但是,如何扩展MYSQL查询以累计user_id具有的202个base_items。例如user_id 41有6.

3 个答案:

答案 0 :(得分:1)

这应该为每个用户ID提供“202”base_items的数量:

SELECT
    user_id,
    COUNT(*)
FROM
    items
WHERE
    base_item = "202"
GROUP BY user_id

答案 1 :(得分:0)

尝试使用类似base_item的count

SELECT
    user_id, 
    base_item, count(*) as user_no
FROM
    items
WHERE
    base_item = "202" 
GROUP BY user_id

答案 2 :(得分:0)

试一试:

SELECT
    user_id,
    count(base_item) as c
FROM
    items
WHERE
    base_item = "202"
group by
    user_id