我使用此查询向我返回一些信息:
select tick.ticketid 'Ticket', tick.subject 'Sujet', FROM_UNIXTIME(tick.dateline) 'Reception', FROM_UNIXTIME(logs1.dateline) 'Verification', FROM_UNIXTIME(logs2.dateline) 'Debut trait.',
FROM_UNIXTIME(logs3.dateline) 'Fin trait.', tick.prioritytitle 'Priorite', tick.tickettypetitle 'Genre', tick.timeworked/60 'Temps de travail', tick.fullname 'Createur'
from swtickets tick, swticketauditlogs logs1, swticketauditlogs logs2, swticketauditlogs logs3
where tick.ticketid = logs1.ticketid
and tick.ticketid = logs2.ticketid
and tick.ticketid = logs3.ticketid
and logs1.actionmsg like '%to: Pre-analysis%'
and (logs2.actionmsg like '%to: Handled%' or logs2.actionmsg like '%to: In Progress%')
and (logs3.actionmsg like '%to: Pre-closed%' or logs3.actionmsg like '%to: Closed%')
group by tick.ticketid
我选择了swticketauditlogs表的3倍,因为对于我的查询的每一行,我需要三倍于该表的相同列但具有其他条件。
我的问题是,使用此代码,如果不遵守表swticketauditlogs中的三个条件之一,则不会选择故障单。我想要的是:
选择了故障单,并且具有不受干扰条件的字段为空。
但我无法想象如何做到这一点......
感谢您的帮助。
答案 0 :(得分:1)
您必须通过显式LEFT JOIN
s替换隐式联接:
...
FROM swtickets tick
LEFT JOIN swticketauditlogs logs1
ON ( logs1.ticketid = tick.ticketid
AND logs1.actionmsg LIKE '%to: Pre-analysis%'
)
LEFT JOIN swticketauditlogs logs2
ON
...
如果找到记录,则返回值,否则您选择的列为NULL
。
答案 1 :(得分:1)
您想使用outer joins(不会消除其他表中不匹配的行):
SELECT
tick.ticketid AS `Ticket`,
tick.subject AS `Sujet`,
FROM_UNIXTIME(tick.dateline) AS `Reception`,
FROM_UNIXTIME(logs1.dateline) AS `Verification`,
FROM_UNIXTIME(logs2.dateline) AS `Debut trait.`,
FROM_UNIXTIME(logs3.dateline) AS `Fin trait.`,
tick.prioritytitle AS `Priorite`,
tick.tickettypetitle AS `Genre`,
tick.timeworked/60 AS `Temps de travail`,
tick.fullname AS `Createur`
FROM
swtickets AS tick
LEFT JOIN swticketauditlogs AS logs1 ON (
logs1.ticketid = tick.ticketid
AND logs1.actionmsg LIKE '%to: Pre-analysis%'
)
LEFT JOIN swticketauditlogs AS logs2 ON (
logs2.ticketid = tick.ticketid
AND (logs2.actionmsg LIKE '%to: Handled%'
OR logs2.actionmsg LIKE '%to: In Progress%')
)
LEFT JOIN swticketauditlogs AS logs3 ON (
logs3.ticketid = tick.ticketid
AND (logs3.actionmsg LIKE '%to: Pre-closed%'
OR logs3.actionmsg LIKE '%to: Closed%')
)
GROUP BY tick.ticketid
答案 2 :(得分:1)
使用LEFT JOIN
s:
select ...
from swtickets tick
left join swticketauditlogs logs1 on tick.ticketid = logs1.ticketid and logs1.actionmsg like '%to: Pre-analysis%'
left join swticketauditlogs logs2 on tick.ticketid = logs2.ticketid and (logs2.actionmsg like '%to: Handled%' or logs2.actionmsg like '%to: In Progress%')
left join swticketauditlogs logs3 on tick.ticketid = logs3.ticketid and (logs3.actionmsg like '%to: Pre-closed%' or logs3.actionmsg like '%to: Closed%')
group by tick.ticketid
答案 3 :(得分:0)
您想使用LEFT JOIN
s:
select tick.ticketid 'Ticket', tick.subject 'Sujet', FROM_UNIXTIME(tick.dateline) 'Reception', FROM_UNIXTIME(logs1.dateline) 'Verification', FROM_UNIXTIME(logs2.dateline) 'Debut trait.',
FROM_UNIXTIME(logs3.dateline) 'Fin trait.', tick.prioritytitle 'Priorite', tick.tickettypetitle 'Genre', tick.timeworked/60 'Temps de travail', tick.fullname 'Createur'
from
swtickets tick
left join
swticketauditlogs logs1
on
tick.ticketid = logs1.ticketid and
logs1.actionmsg like '%to: Pre-analysis%'
left join
swticketauditlogs logs2
on
tick.ticketid = logs2.ticketid and
(logs2.actionmsg like '%to: Handled%' or logs2.actionmsg like '%to: In Progress%')
left join
swticketauditlogs logs3
on
tick.ticketid = logs3.ticketid and
(logs3.actionmsg like '%to: Pre-closed%' or logs3.actionmsg like '%to: Closed%')
group by tick.ticketid
如果连接不成功,则左连接会在右表的列中生成空值。 (请注意,这些 为空,而不是“空白” - 无论这意味着什么)。