我有一个带有多个连接的SQL语句,可以完全没有任何问题
$sql = "SELECT ni.*, GROUP_CONCAT(DISTINCT newsletter_item_receivers.value ORDER BY newsletter_item_receivers.value) AS receivers, nf.*, GROUP_CONCAT(DISTINCT nm.mailgroup_name ORDER BY nm.mailgroup_name) AS mailgroups
FROM newsletter_items ni
INNER JOIN newsletter_fields nf ON (nf.field_letter_uid = ni.letter_id)
LEFT JOIN newsletter_item_receivers ON (newsletter_item_receivers.letter_id = ni.letter_id)
INNER JOIN newsletter_mailgroups nm ON (FIND_IN_SET(nm.mailgroup_id, newsletter_item_receivers.value))
WHERE nf.field_name = 'letter_headline' AND ni.template = '". $template ."' AND deleted = 0
GROUP BY ni.letter_id
我想要的是加入包含ifo的newsletter_clickstat中有关特定时事通讯链接的数据(例如链接点击次数)。
我试图插入
LEFT JOIN newsletter_clickstat cs ON (cs.letter_id = ni.letter_id)
但那并没有做任何事情
解决:
SELECT ni.*, GROUP_CONCAT(DISTINCT newsletter_item_receivers.value ORDER BY newsletter_item_receivers.value) AS receivers, nf.*, GROUP_CONCAT(DISTINCT nm.mailgroup_name ORDER BY nm.mailgroup_name) AS mailgroups, cs.*
FROM newsletter_items ni
INNER JOIN newsletter_fields nf ON (nf.field_letter_uid = ni.letter_id)
LEFT JOIN newsletter_item_receivers ON (newsletter_item_receivers.letter_id = ni.letter_id)
INNER JOIN newsletter_mailgroups nm ON (FIND_IN_SET(nm.mailgroup_id, newsletter_item_receivers.value))
LEFT JOIN newsletter_clickstat cs ON (cs.letter_id = ni.letter_id)
WHERE nf.field_name = 'letter_headline' AND ni.template = '". $template ."' AND deleted = 0
GROUP BY ni.letter_id
答案 0 :(得分:1)
(升级到答案)
您还需要从结果集中选择newsletter_clickstat
中的列...请记住您正在执行分组操作,因此需要应用聚合函数或更改列你执行你的小组。