我正在尝试返回一个结果集,其中包含另一个表中动态字段的分隔列表。它似乎正在工作,但即使我多次复制同一行,结果也只返回一行。
这是我的疑问:
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或者每个导出规则的连接有多行的事实是抛出它关了。
答案 0 :(得分:2)
我通过使用LEFT JOIN而不是常规JOIN来解决它。当我之前测试过它时,我没有GROUP BY export_rules.id。
此问题已解决。