2013-02-15 101 views
1

我想返回一個結果集,其中包含另一個表中動態字段的分隔列表。它似乎工作,但結果只返回一行,即使我複製同一行多次。加入GROUP_CONCAT只返回1行

這裏是我的查詢:

SELECT export_rules.*, GROUP_CONCAT(CAST(CONCAT(export_rule_fields.key, ': ', export_rule_fields.value) AS char) SEPARATOR '|') AS fields FROM export_rules 
JOIN export_rule_fields ON export_rules.id = export_rule_fields.export_rule_id 
WHERE export_rules.output_type = "email" 
AND (export_rules.frequency = "daily" 
    AND export_rules.last_execution < TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) 
    AND (export_rules.cron_expression = "25 11 * * *" 
     OR export_rules.cron_expression = "30 11 * * *")) 
OR (export_rules.frequency = "weekly" 
    AND export_rules.last_execution < TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 WEEK)) 
    AND (export_rules.cron_expression = "25 11 * * 5" 
     OR export_rules.cron_expression = "30 11 * * 5")) 
OR (export_rules.frequency = "monthly" 
    AND export_rules.last_execution < TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH)) 
    AND (export_rules.cron_expression = "25 11 15 * *" 
     OR export_rules.cron_expression = "30 11 15 * *")) 
GROUP BY export_rules.id 

它應該由它們相應的表達搶crons的列表(部分作品),然後將它們吐出的田地,但我認爲GROUP_CONCAT或事實對於每個導出規則而言,聯接都有多行將其拋出。

+3

或者它是'group by'不工作,因爲你期待它? – 2013-02-15 18:40:56

回答

2

我通過使用LEFT JOIN而不是常規JOIN來解決它。當我測試過之前,我沒有GROUP BY export_rules.id。

此問題已解決。