在使用 MySQL 查询为 Kayako Fusion 生成一些外部报告时,我遇到了一个奇怪而烦人的问题。表中的一个字段包含一个“TEXT”字段,该字段可以包含以下两个值之一:41 或 40。如果最终用户未选择其中一个值,则默认值为 NULL。(我无法改变它)。
所以,当试图做一个简单的:
WHERE swcustomfieldvalues.fieldvalue = '41'
然后它还返回具有 NULL(或者可能是 '')值的记录。
这同样适用于:
WHERE swcustomfieldvalues.fieldvalue = '40'
我们还获得了 NULL 记录(这有点准确,因为我们将 NULL 视为 40。
为简单起见,40 的值用于非收费工作,41 的值用于收费工作。
我错过了什么,或者有解决方法吗?
这是讨厌的原始 SQL 语句:
SELECT
swtickets.ticketid AS `Ticket ID`,
swtickettimetracks.tickettimetrackid AS `Track ID`,
swtickets.ticketmaskid AS `TicketMASK`,
(
SELECT
swcustomfieldvalues.fieldvalue
FROM
swcustomfieldvalues,
swcustomfields
WHERE
swcustomfieldvalues.customfieldid = swcustomfields.customfieldid
AND swtickets.ticketid = swcustomfieldvalues.typeid
AND swcustomfields.title = 'Member Company'
ORDER BY
swcustomfieldvalues.customfieldvalueid DESC
LIMIT 1
) AS MemberCompany,
(
SELECT
swcustomfieldvalues.fieldvalue
FROM
swcustomfieldvalues,
swcustomfields
WHERE
swcustomfieldvalues.customfieldid = swcustomfields.customfieldid
AND swtickets.ticketid = swcustomfieldvalues.typeid
AND swcustomfields.title = 'Member Name'
ORDER BY
swcustomfieldvalues.customfieldvalueid DESC
LIMIT 1
) AS MemberName,
(
SELECT
swcustomfieldvalues.fieldvalue
FROM
swcustomfieldvalues,
swcustomfields
WHERE
swcustomfieldvalues.customfieldid = swcustomfields.customfieldid
AND swtickets.ticketid = swcustomfieldvalues.typeid
AND swcustomfields.title = 'Chargeable'
AND swcustomfieldvalues.fieldvalue = '41'
ORDER BY
swcustomfieldvalues.customfieldvalueid ASC
LIMIT 1
) AS `Chg`,
swtickets.`subject` AS `Subject`,
swtickets.departmenttitle AS Category,
FROM_UNIXTIME(
swtickettimetracks.workdateline,
'%Y-%m-%d'
) AS `workDateline`,
FROM_UNIXTIME(
swtickettimetracks.dateline,
'%Y-%m-%d'
) AS `dateline`,
swtickettimetracks.timespent AS `Time Spent`,
swtickets.timeworked / 60 AS `Time Worked`
FROM
swtickets
RIGHT OUTER JOIN swusers ON swtickets.userid = swusers.userid
INNER JOIN swuserorganizations ON swuserorganizations.userorganizationid = swusers.userorganizationid
INNER JOIN swtickettimetracks ON swtickettimetracks.ticketid = swtickets.ticketid
WHERE
swuserorganizations.organizationname = 'Clarence Professional Offices'
AND (
swtickets.ticketstatustitle = 'Closed'
OR swtickets.ticketstatustitle = 'Completed'
)
AND swtickets.ticketid = '2895'
GROUP BY
`Ticket ID`,
`Track ID`
请善待我,我是新手;)
打破个别的子选择(这是不可避免的)我可以证明这个用于获取自定义字段“收费”的个人选择与特定票“2895”的值为“41”实际上有效!看:
SELECT
swcustomfieldvalues.fieldvalue,
swtickets.ticketid
FROM
swcustomfieldvalues,
swcustomfields,
swtickets
WHERE
swcustomfieldvalues.customfieldid = swcustomfields.customfieldid
AND swtickets.ticketid = swcustomfieldvalues.typeid
AND swcustomfields.title = 'Chargeable'
AND swcustomfieldvalues.fieldvalue = '40'
AND swtickets.ticketid = '2895'
ORDER BY
swcustomfieldvalues.customfieldvalueid ASC
它返回 TRUE,因为我知道这张票是不收费的 (40)。如果我将值更改为“41”(收费),它会返回 False(无记录)。
整个 SQL 语句(原始)应返回:所有在某个日期范围内可收费的票,显示给我
票证 ID/票证掩码、跟踪 ID、会员公司、会员姓名、是否收费?(40|41)、主题、类别、工作日期、日期、花费时间和工作时间。
然后在 PHP 中,我做了一些更多的操作来为 Sub Totals 等做一个“Break On”。但我离题了。