MySQL - 使用嵌套分组的嵌套选择

时间:2014-08-23 04:28:37

标签: mysql sql

我有两个表,我正在构建一些统计信息,因此我计算了多个列和表,我的问题是当我尝试加入另一个表并将其分组到嵌套语句中时,我不断得到以下内容我运行代码时出错。

General error: 2014 Cannot execute queries while other unbuffered queries are active...

这是情景:

++============================================================================++
||                               CLIENTS TABLE                                ||
++=======+===============+==============+==============+======================++
|   id   |   firstname   |   lastname   |     email    |      created_at       |
+--------+---------------+--------------+--------------+-----------------------+
|    1   |     JOHN      |      DOE     | john@doe.com |  2014-08-22 20:10:30  |
+--------+---------------+--------------+--------------+-----------------------+
|    2   |     JUNE      |      DAE     | june@dae.com |  2014-07-28 18:12:08  |
+--------+---------------+--------------+--------------+-----------------------+

++============================================================================++
||                               PURCHASES TABLE                              ||
++=======+===============+=============================+======================++
|   id   |   client_id   |     transaction_status      |      created_at       |
+--------+---------------+-----------------------------+-----------------------+
|    1   |        1      |          COMPLETED          |  2014-08-22 20:10:30  |
+--------+---------------+-----------------------------+-----------------------+
|    2   |        2      |          INCOMPLETE         |  2014-08-22 20:10:30  |
+--------+---------------+-----------------------------+-----------------------+
|    1   |        2      |          COMPLETED          |  2014-08-22 20:10:30  |
+--------+---------------+-----------------------------+-----------------------+
|    2   |        1      |          COMPLETED          |  2014-08-22 20:10:30  |
+--------+---------------+-----------------------------+-----------------------+

这是我计算的一些事情:

  1. 全球客户总数
  2. 客户当月总数
  3. 过去一个月的客户总数
  4. 最后注册的客户
  5. 已完成购买的客户(此操作失败)
  6. 最后这是我失败的疑问:

    SELECT 
      ( SELECT 
          COUNT(*) 
        FROM
          clients
      ) AS 
          total_registered_clients,
    
      ( SELECT 
          COUNT(*) 
        FROM 
          clients 
        AND 
          (YEAR(created_at) = YEAR(CURRENT_DATE)) 
        AND 
          (MONTH(created_at) = MONTH(CURRENT_DATE)) 
      ) AS 
          current_month_registered_clients,
    
      ( SELECT 
          COUNT(*) 
        FROM 
          clients 
        AND 
          created_at 
        BETWEEN 
          (CURRENT_DATE - INTERVAL 1 MONTH) 
        AND 
          CURRENT_DATE 
      ) AS 
          last_month_registered_clients,
    
      -- This part fails
      ( SELECT
          COUNT(*)
        FROM
          clients
        INNER JOIN
          purchases
        WHERE
          purchases.client_id = clients.id
        AND
          purchases.transaction_status = 'completed'
        GROUP BY
          purchases.client_id
       ) AS
           clients_with_purchases
    

    编辑: 我对var_dump的预期结果是:

    [0] =>
    object(stdClass)#60 (10) {
      ["total_registered_clients"]=>
        string(1) "2"
      ["current_month_registered_clients"]=>
        string(1) "1"
      ["last_month_registered_clients"]=>
        string(1) "1"
      ["clients_with_purchases"]=>
        string(1) "2"
    

    }

2 个答案:

答案 0 :(得分:1)

删除该错误:

INNER JOIN
      purchases
    ON    -- not WHERE
      purchases.client_id = clients.id

并删除GROUP BY

但是,第三个查询是否试图计算已购买的客户数量或购买数量? (你在计算购买量)

计算已购买客户数量的2种方法

SELECT
      COUNT(DISTINCT clients.id)
FROM clients
      INNER JOIN purchases
                  ON purchases.client_id = clients.id
                        AND purchases.transaction_status = 'completed'

SELECT
      COUNT(*)
FROM clients
WHERE EXISTS (
            SELECT
                  1
            FROM purchases
            WHERE transaction_status = 'completed'
                  AND clients.id = purchases.client_id
      )

答案 1 :(得分:0)

SELECT 
  ( SELECT 
      COUNT(*) 
    FROM
      clients
  ) AS 
      total_registered_clients,

  ( SELECT 
      COUNT(*) 
    FROM 
      clients 
    where
      (YEAR(created_at) = YEAR(GETDATE())) 
    AND 
      (MONTH(created_at) = MONTH(GETDATE())) 
  ) AS 
      current_month_registered_clients,

  ( SELECT 
      COUNT(*) 
    FROM 
      clients 
    where 
      created_at 
    BETWEEN 
      (dateadd(m,-1,GETDATE())  ) 
    AND 
      GETDATE() 
  ) AS 
      last_month_registered_clients,

  -- This part fails
  ( SELECT
      COUNT(*)
    FROM
      clients
    INNER JOIN
      purchases
    on
      purchases.client_id = clients.id
    where
      purchases.transaction_status = 'completed'

   ) AS
       clients_with_purchases