为什么LEFT JOIN会破坏我的MySQL查询?

时间:2016-04-11 18:21:16

标签: mysql sql join

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

我正在尝试编辑此查询,以根据第一部分中返回的用户添加付款的总和值。评论文本是我添加的内容。它正如预期的那样工作,但是当我添加我的部分来获取它时,它会破坏它。

任何建议都会非常棒,谢谢。我很喜欢加入SQL和SQL。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

好的,这才是最终奏效的。谢谢你的帮助!

SELECT值AS $ point_fld,        COUNT(DISTINCT u.user_id)作为cnt,        SUM(p.amount)为moneysum
FROM?_data d LEFT JOIN?_user u ON d。table ='user'AND d。id = u.user_id LEFT JOIN?_invoice_payment p ON d。id = p.user_id 在哪里key =?和value&lt;&gt;''和value&lt;&gt;'空白'和       你加入过吗?和? GROUP BY值