2016-04-11 105 views
0
SELECT value AS $point_fld, COUNT(value) as cnt 
FROM ?_data d LEFT JOIN 
    ?_user u 
    ON d.`table` = 'user' AND d.`id` = u.user_id 
WHERE `key` = ? AND `value`<>'' AND `value`<>'Blank' AND 
     u.added BETWEEN ? AND ? 

/* ----- I added this bit */ 
LEFT JOIN ?_invoice_payment p 
ON d.'id' = p.user_id 
SUM(p.amount) as moneysum 
/* End ---- */ 
GROUP BY $point_fld 

我試圖編輯這個查詢,以在第一部分返回用戶的付款總額中添加付款金額。評論文字是我添加的。它正在按照預期工作,但是當我加入我的部分來獲取它打破它的值時。爲什麼LEFT JOIN會破壞我的MySQL查詢?

任何建議將是驚人的謝謝。我對SQL中的連接和東西很陌生。

+2

'LEFT JOIN'是僅在'FROM'子句中使用的關鍵字。 –

+0

那麼這意味着我將不得不在一個完全獨立的查詢中做到這一點?或者有沒有辦法在這個相同的查詢中做我想做的事情? – elrobbo6

+0

我從來沒有見過'?_xxx'符號,你是否試圖參數化表名?允許哪裏? – Uueerdo

回答

0
SELECT value AS $point_fld, 
     COUNT(value) as cnt, 
     SUM(p.amount) as moneysum 
FROM ?_data d 
INNER JOIN ?_user u ON d.`table` = 'user' AND d.`id` = u.user_id 
LEFT JOIN ?_invoice_payment p ON d.'id' = p.user_id 
WHERE `key` = ? AND `value`<>'' AND `value`<>'Blank' AND 
     u.added BETWEEN ? AND ? 
GROUP BY value 
+0

我會給這個試試謝謝 – elrobbo6

+0

這仍然沒有工作:/ – elrobbo6

+1

這是什麼使它不起作用? –

0

確定這是最後的工作。謝謝您的幫助!

SELECT值point_fld $, COUNT(DISTINCT u.user_id)爲CNT, SUM(p.amount)作爲moneysum
而來?_data d LEFT JOIN?_user U ON d。 table ='用戶'和d。 id = u.user_id LEFT JOIN?_invoice_payment p ON d。 id = p.user_id WHERE key =? AND value <>''AND value <>'Blank'AND u.added BETWEEN?和? GROUP BY值