如何在子查詢中引用父查詢列?這裏是我的查詢#1054 - 'where子句'中的未知列'XXXX'
SELECT
c.username, c.staff_of, CONCAT(c.first_name, ' ', c.middle_name, ' ', c.last_name) AS csr,
d.date, d.account, d.total_transaction, d.total_participate, d.total_amount,
(SELECT COUNT(id) FROM users WHERE created_by = c.username) AS total_agents
FROM `users` c,
(SELECT
DATE(res_date) AS date, B.res_account AS account,
COUNT(B.res_id) AS total_transaction,
COUNT(DISTINCT(B.res_account)) AS total_participate,
SUM(B.res_amount) AS total_amount
FROM
merchant_responses B
WHERE B.res_account IN (SELECT t.staff_of FROM users t WHERE t.created_by = c.username)
) AS d
WHERE c.account_type ='DSO' GROUP BY c.username
,這是結果我得到
#1054 - Unknown column 'c.username' in 'where clause'
我如何c.username可見的子查詢?
Thanx in advance
隊友,請使用JOIN。 theres不需要所有這些子查詢。效率低下且存在問題 – ldgorman 2013-05-03 12:47:08
派生表「d」與「用戶」表之間缺少連接條件。這將創建笛卡爾連接。此外,在派生表內使用聚合在標準SQL中是非法的。 MySQL會讓你逃脫 - 但它會返回隨機值:http://rpbouman.blogspot.de/2007/05/debunking-group-by-myths.html – 2013-05-03 12:49:13