我有两个表,我正在构建一些统计信息,因此我计算了多个列和表,我的问题是当我尝试加入另一个表并将其分组到嵌套语句中时,我不断得到以下内容我运行代码时出错。
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 |
+--------+---------------+-----------------------------+-----------------------+
这是我计算的一些事情:
最后这是我失败的疑问:
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"
}
答案 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