简体   繁体   English

MySQL-带嵌套分组的嵌套选择

[英]MySQL - Nested select with nested grouping

I have two tables, and I am building some statistics from it, so I am counting multiple columns and tables, my issue is when I try to join another table and group it in the nested statement, and I keep getting the following error when I run my code. 我有两个表,并且正在从中建立一些统计信息,所以我要计算多个列和表,我的问题是当我尝试联接另一个表并将其分组到嵌套语句中时,当我不断出现以下错误时运行我的代码。

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

This is the scenario: 这是方案:

++============================================================================++
||                               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  |
+--------+---------------+-----------------------------+-----------------------+

This are some of the things I'm counting: 这是我正在数的一些事情:

  1. Total clients globally 全球客户总数
  2. Total clients current month 本月客户总数
  3. Total clients past month 过去一个月的客户总数
  4. Last registered client 最后注册的客户
  5. Client with completed purchases (This fails) 已完成购买的客户(此操作失败)

And finally this is my failing query: 最后这是我失败的查询:

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

EDIT: My expected result with var_dump is: 编辑:我与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"

} }

To remove that error: 要消除该错误:

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

and remove the GROUP BY 并删除GROUP BY

But in that third query are trying to count the number of clients who have purchased or the number of purchases? 但是在第三个查询中,您是否要计算已购买的客户数量或购买数量? (you are counting purchases) (您正在计算购买量)

2 methods for counting number of clients who have purchased 两种计算已购买客户数量的方法

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
      )
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM