我安装了trac来管理我的在线项目。我还在其中安装了日期插件DateFieldPlugin。我无法在自定义报告中显示此日期。
以下代码生成正常报告。
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,reporter AS _reporter
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
我想要这样的事情:
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter, c.value AS Duedate
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority', ticket_custom c
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
上面的代码显示了截止日期列,尽管它多次显示一个任务行。
有人可以帮助我吗?
答案 0 :(得分:7)
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter, c.value AS Duedate
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT OUTER JOIN ticket_custom c ON t.id = c.ticket AND c.name = 'due_date'
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
只有您必须替换 c.name 值才能与您的日期字段名称匹配,如果它不像我的配置中那样'due_date'。
背景:'ticket_custom'表定义只是每行一个键值对,因为对模式的调用显示:
sqlite> .schema ticket_custom
CREATE TABLE ticket_custom (
ticket integer,
name text,
value text,
UNIQUE (ticket,name)
);
请注意主键不仅仅是票号,还包括票证(号码)和(自定义列)名称。因此,JOIN上需要一个额外的WHERE子句,或者您仍然会获得多行 - 每个匹配票证的每个自定义字段对应一行。这与主表'ticket'形成对比,其中一张票的所有信息都在一行中。
答案 1 :(得分:2)
您没有在ticket_custom
上指定加入条件。因此,它正在执行CROSS JOIN
,它将ticket_custom
中的每条记录连接到查询其余部分的每条记录。因此,如果您的ticket left join enum
最初返回100行,而ticket_custom
有20行,那么您的第二个查询中将产生2000行。
您需要确定ticket_custom与其他表的关系,并提供适当的谓词。最有可能的是,它是这样的:
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter, c.value AS Duedate
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT JOIN ticket_custom c ON t.id = c.ticket_id
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
(参见新的LEFT JOIN行)。这些列是猜测 - 您需要替换正确关联2个表的列。