0

我希望这是寻求帮助的适当论坛。我有一个 SQL 查询 (MySQL),它没有在日期范围内(两个日期之间)返回正确的记录。我很高兴回答与查询有关的问题,但是如果有人可以提出建议或更正 SQL 查询,那将是一个很好的学习练习。谢谢你。

$raw_query = sprintf("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 = '40'
    ORDER BY
        swcustomfieldvalues.customfieldvalueid ASC
    LIMIT 1
) AS `Chg`,
swtickets.`subject` AS `Subject`,
swtickets.departmenttitle AS Category,
FROM_UNIXTIME(
    swtickettimetracks.workdateline
) AS `workDateline`,
FROM_UNIXTIME(
    swtickettimetracks.dateline
) AS `dateline`,
swtickettimetracks.timespent AS `Time Spent`,
swtickets.timeworked AS `Time Worked`
    FROM
swtickets
    INNER 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 = '%s'
    AND (
    swtickets.ticketstatustitle = 'Closed'
OR swtickets.ticketstatustitle = 'Completed'
    )

    AND FROM_UNIXTIME(`workDateline`) >= '%s' AND FROM_UNIXTIME(`workDateline`) <= '%s'

    ORDER BY `Ticket ID`,`Track ID`",
        $userOrganization,
        $startDate,
        $endDate
    );

正如我所提到的,查询有效 - 但是它不会在两个日期之间正确返回记录。

但是,如果我对数据库运行这个简单的查询:

SELECT swtickettimetracks.tickettimetrackid, 
swtickettimetracks.ticketid, 
swtickettimetracks.dateline, 
swtickettimetracks.timespent, 
swtickettimetracks.timebillable,
    FROM_UNIXTIME(swtickettimetracks.workdateline)

    FROM swtickettimetracks

    WHERE FROM_UNIXTIME(swtickettimetracks.workdateline) >= '2013-04-16' AND FROM_UNIXTIME(swtickettimetracks.workdateline) <= '2013-04-18'

我得到了正确的日期范围返回。帮助?谢谢你的期待。爱德华。

4

1 回答 1

0

除非你想得太多,否则一切都在你不同的查询 WHERE 子句中......

您返回错误结果的复杂查询有

  (join conditions between other tables)
  AND swuserorganizations.organizationname = '%s'
  AND ( swtickets.ticketstatustitle = 'Closed'
     OR swtickets.ticketstatustitle = 'Completed' )
  AND FROM_UNIXTIME(`workDateline`) >= '%s' 
  AND FROM_UNIXTIME(`workDateline`) <= '%s'

您的其他查询有

FROM swtickettimetracks
WHERE FROM_UNIXTIME(swtickettimetracks.workdateline) >= '2013-04-16' 
  AND FROM_UNIXTIME(swtickettimetracks.workdateline) <= '2013-04-18'

所以我会考虑一些事情。第一个哪里有

FROM_UNIXTIME >= '%s' 和 FROM_UNIXTIME <= '%s'

您确定 '%s' 值的格式正确以匹配 '2013-04-16' 和 '2013-04-18' 格式示例吗?

但更重要的是,您的第一个查询使用相同的日期范围(如果正确),但也只获取特定组织名称和(已关闭或已完成)记录的那些。那么,如果第二个查询返回 100 条记录,但主查询只返回 70 条,那么其他 30 条是不是已关闭/已完成,或者是不同的组织?此外,如果连接表没有匹配的 ID,则会阻止返回具有无效 ID 的那些。确认这一点的唯一方法是在这些表上更改为 LEFT-JOIN 语法并查看结果。

于 2013-04-19T11:52:51.657 回答