简单的mySQL查询失败

时间:2013-05-23 08:36:17

标签: mysql

我有以下mySQL查询,由于某种原因,我无法使其正常工作。

我有两张桌子:

exp_competition_purchase_upload_total包含成员标识和total_uploads列。 exp_channel_titles有很多列,但我需要检查的是作者ID和通道ID。

我的目标是查询第一个表并找到任何行,找到member_id并获取与之关联的upload_total。然后转到第二个表,计算具有与表1中的成员ID匹配的作者ID的行,并且通道ID为1。

SELECT COUNT(*) AS uploads, c.upload_total 
FROM exp_competition_purchase_upload_total AS c
LEFT JOIN exp_channel_titles AS t ON c.member_id = t.author_id
WHERE c.member_id = '1'
AND t.channel_id = '1'

目前计数正在返回,但不是upload_total数。以下作品:

SELECT upload_total 
FROM exp_competition_purchase_upload_total
WHERE member_id = '1'

enter image description here

谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT 
  COUNT(*) AS uploads, 
  SUM(IFNULL(c.upload_total,0)) 
FROM 
  exp_competition_purchase_upload_total AS c
LEFT JOIN exp_channel_titles AS t 
  ON c.member_id = t.author_id
  AND t.channel_id = '1'
WHERE 
  c.member_id = '1'