简体   繁体   English

协助进行复杂的MySQL SQL查询

[英]Assistance with a complex MySQL SQL Query

I hope this is the appropriate forum to ask for assistance. 我希望这是寻求帮助的合适论坛。 I have an SQL Query (MySQL) that is not returning the correct records in a Date Range (between two dates). 我有一个SQL查询(MySQL),它没有在日期范围(两个日期之间)中返回正确的记录。 I am happy to answer questions in relation to the query, however if anyone can make suggestions or correct the SQL Query that would be an excellent learning exercise. 我很高兴回答与查询有关的问题,但是,如果有人可以提出建议或更正SQL Query,那将是一个很好的学习方法。 Thank you. 谢谢。

$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
    );

As I mentioned, the Query works - however it does not return the records correctly between the two dates. 正如我提到的,查询有效-但是它不能在两个日期之间正确返回记录。

However, IF I run this simple query against the database : 但是,如果我对数据库运行以下简单查询:

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'

I get the correct date range returned. 我得到正确的日期范围返回。 Help? 救命? Thank you in anticipation. 谢谢您的期待。 Edward. 爱德华

Unless you are overthinking it, it's all in your different query WHERE clauses... 除非您考虑过度,否则所有操作都在您不同的查询WHERE子句中。

Your complex query returning the wrong results has 您的复杂查询返回了错误的结果,

  (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'

Your Other query has 您的其他查询有

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

So I would consider a few things. 所以我会考虑一些事情。 The first where has 第一个有

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

Are you sure the '%s' values are properly formatted to match the '2013-04-16' and '2013-04-18' format sample? 您确定'%s'值的格式正确以匹配'2013-04-16'和'2013-04-18'格式示例吗?

But more importantly, your first query is using the same date range (if correct), but is also only getting those for specific organization name AND (Closed or Completed) records. 但更重要的是,您的第一个查询使用的是相同的日期范围(如果正确),但也仅获取特定组织名称AND(关闭或完成)记录的日期范围。 So, if the second query is returning 100 records, but the main query only 70, then are the other 30 some status other than closed/completed, or a different organization? 因此,如果第二个查询返回100条记录,但主查询仅返回70条记录,那么其他30条状态是否为已关闭/已完成或其他组织? In addition, if the join tables don't have matching IDs that would prevent those with invalid IDs from being returned. 此外,如果联接表没有匹配的ID,则将阻止返回具有无效ID的ID。 The only way to confirm that is to change to LEFT-JOIN syntax on those tables and see the results. 确认的唯一方法是更改​​这些表上的LEFT-JOIN语法并查看结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM