0

我有一个带有多个连接的 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

我想要的是加入来自 newsletter_clickstat 的数据,其中包含有关特定时事通讯上链接的 ifo(例如链接上的点击次数)。

我试图插入

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
4

1 回答 1

1

升级到答案

您还需要newsletter_clickstat在结果集中选择您想要的列...请记住,您正在执行分组操作,因此需要应用聚合函数或更改您执行分组的列。

于 2012-05-31T14:09:11.590 回答