MySQL - 计算出售ITEMS的次数 - 管理员统计页面

时间:2016-05-23 13:41:11

标签: mysql

我有以下 solditems

+---------+--------+----------+
| orderid | itemid | unitsold |
+---------+--------+----------+
| 1       | ITEM1  | 2        |
+---------+--------+----------+
| 2       | ITEM2  | 5        |
+---------+--------+----------+
| 3       | ITEM1  | 1        |
+---------+--------+----------+
| 4       | ITEM3  | 1        |
+---------+--------+----------+

我需要在我的统计信息页面上找到以下值。

  1. 销售总量
  2. 仅售出一次的商品总数
  3. 多次售出的商品总数
  4. 查询第一个问题

    SELECT sum(unitsold) AS total FROM solditems;
    

    对于第二个问题,我使用以下查询

    SELECT 
        itemid, SUM(unitsold) AS total
    FROM
        solditems
    GROUP BY itemid
    ORDER BY total DESC;
    

    获得以下结果

    +--------+-------+
    | itemid | total |
    +--------+-------+
    | ITEM2  | 5     |
    +--------+-------+
    | ITEM1  | 3     |
    +--------+-------+
    | ITEM3  | 1     |
    +--------+-------+
    

    对于问题2,我如何得到答案 1 ,因为只有一个项目 ITEM3 只卖一次

    对于问题3,我如何得到答案 2 ,因为两个 ITEM1 ITEM2 多次出售

    我可以在查询中使用“HAVING”列出一次又一次销售的商品。

    SELECT 
        itemid, SUM(unitsold) AS total
    FROM
        solditems
    GROUP BY itemid
    HAVING total = 1
    ORDER BY total DESC;
    

    获取仅售出一次的商品。

    +--------+-------+
    | itemid | total |
    +--------+-------+
    | ITEM3  | 1     |
    +--------+-------+
    

    SELECT 
        itemid, SUM(unitsold) AS total
    FROM
        solditems
    GROUP BY itemid
    HAVING total > 1
    ORDER BY total DESC;
    

    获取多次售出的商品。

    +--------+-------+
    | itemid | total |
    +--------+-------+
    | ITEM2  | 5     |
    +--------+-------+
    | ITEM1  | 3     |
    +--------+-------+
    

    我应该如何修改我的查询以获得以下答案。

    问题2:答案总数= 1 [即仅出售一次的物品总数]

    问题3:答案总数= 2 [即多次出售的物品总数]

    ANSWER

    问题2:

    SELECT 
        COUNT(DISTINCT itemid) AS total
    FROM
        solditems
    WHERE
        itemid IN (SELECT 
                itemid
            FROM
                solditems
            GROUP BY itemid
            HAVING SUM(unitsold) = 1);
    

    问题3:

    SELECT 
        COUNT(DISTINCT itemid) AS total
    FROM
        solditems
    WHERE
        itemid IN (SELECT 
                itemid
            FROM
                solditems
            GROUP BY itemid
            HAVING SUM(unitsold) > 1);
    

    我得到答案,但想检查是否有更好的方法来做到这一点。

4 个答案:

答案 0 :(得分:1)

  

您可以从MySQL中的选择

计算
SELECT 
    COUNT(*) AS amount_sold_once
FROM
    (SELECT 
        itemid, SUM(unitsold) AS total
    FROM
        solditems
    GROUP BY itemid
    HAVING total = 1
    ORDER BY total DESC) AS sold_once

请注意您要计算的select语句 在圆括号()中,并有自己的别名。

答案 1 :(得分:0)

您必须在查询中添加HAVING部分。

    SELECT 
        itemid, SUM(unitsold) AS total
    FROM
        solditems
    GROUP BY itemid HAVING SUM(unitsold) > 1
    ORDER BY total DESC;

答案 2 :(得分:0)

对于您的问题2.尝试此查询。

SELECT 
    itemid, count(itemid) AS total
FROM
    solditems
GROUP BY itemid HAVING  total= 1

对于问题3,试试这个

SELECT 
    itemid, count(itemid) AS total
FROM
    solditems
GROUP BY itemid HAVING  total>1

答案 3 :(得分:0)

您可以尝试此查询

select itemid ,sum(unitsold) as unitsold from solditems 
where itemid in(select distinct(itemid )
 from solditems) orderby unitsold asc;