需要加入3个表。需要按DateTime字段排序并限制为1

我有这些表:

CUSTOMERS

Customer ID | Customer Name
----------------------------
1000        | Jonny Ltd.
1001        | James Ltd.
TICKETS

Ticket ID | Ticket Customer | Ticket Subject
---------------------------------------------
10         | 1000            | Testing Sub
11         | 1001            | Testing Sub 2 
12         | 1001            | Testing Sub 3 
Notes

Note ID | Note Ticket ID | Note Content   | Note Created
---------------------------------------------------------------
1       | 10             | Testing Note1. | 2019-04-20 13:38:16
2       | 10             | Testing Note2. | 2019-04-20 12:52:36
3       | 11             | Testing Note3. | 2019-04-19 10:21:54

我想显示每张票,按最近的票据排序。 当我运行此命令时,它没有给我正确的响应。

SELECT `Ticket ID`,`Customer Name`, `Ticket Subject`, `Note Content`, `Note Created`
FROM TICKETS t, CUSTOMERS c, NOTES n
WHERE t.`Ticket Customer` = c.ID AND n.`Note Ticket ID` = t.`Ticket ID`
ORDER BY n.`Note Created` DESC;

我希望它输出:

Ticket ID  | Company Name | Ticket Subject | Note Content   | Note Created
---------------------------------------------------------------------------
10         | Jonny Ltd.   | Testing Sub    | Testing Note1. | 2019-04-20 13:38:16
11         | James Ltd.   | Testing Sub 2  | Testing Note3. | 2019-04-19 10:21:54
12         | James Ltd.   | Testing Sub 3  | NULL           | NULL

2
投票

要获取最新的票据数据,您需要确定每张票证的最新票据日期。您可以使用此查询执行此操作:

SELECT `Note Ticket ID`, MAX(`Note Created`) AS `Note Created`
FROM Notes
GROUP BY `Note Ticket ID`

然后,您可以将该查询用作派生表,以从最新的注释中选择所有数据,并将JOIN用于CustomersTickets表:

SELECT t.`Ticket ID`, c.`Customer Name`, t.`Ticket Subject`,
       n.`Note Content`, n.`Note Created`
FROM Customers c
LEFT JOIN Tickets t ON t.`Ticket Customer` = c.`Customer ID`
LEFT JOIN (SELECT `Note Ticket ID`, MAX(`Note Created`) AS `Note Created`
           FROM Notes
           GROUP BY `Note Ticket ID`) nc ON nc.`Note Ticket ID` = t.`Ticket ID`
LEFT JOIN Notes n ON n.`Note Ticket ID` = nc.`Note Ticket ID` AND
                     n.`Note Created` = nc.`Note Created`
ORDER BY n.`Note Created` DESC

输出:

Ticket ID   Ticket Subject  Customer Name   Note Content    Note Created
10          Testing Sub     Jonny Ltd.      Testing Note1.  2019-04-20 13:38:16
11          Testing Sub 2   James Ltd.      Testing Note3.  2019-04-19 10:21:54
12          Testing Sub 3   James Ltd.      null            null

Demo on dbfiddle

2
投票

您需要子查询来标识每个故障单的最新备注。此版本将子查询放在ON子句中:

SELECT `Ticket ID`, `Customer Name`, `Ticket Subject`, `Note Content`, `Note Created`
FROM TICKETS t JOIN
     CUSTOMERS c
     ON t.`Ticket Customer` = c.ID LEFT JOIN
     NOTES n
     ON n.`Note Ticket ID` = t.`Ticket ID` AND
        n.`Note Created` = (SELECT MAX(n2.`Note Created`)
                            FROM NOTES n2
                            WHERE n2.`Note Ticket ID` = t.`Ticket ID`
                           )
ORDER BY (n.`Note Created` IS NOT NULL) DESC,
         n.`Note Created` DESC;

笔记:

切勿在 FROM条款中使用逗号。始终使用正确,明确,标准的 JOIN语法。 使用 LEFT JOIN,所以你得到没有备注的门票。 我建议重命名列,使它们不包含空格。不必转义标识符将使编写查询更简单。 ORDER BY需要确保 NULL值是最后的。 DESC确实将 NULL值放在最后,但为了以防万一,我为此添加了明确的逻辑。

在MySQL 8+中,您将使用窗口函数:

SELECT `Ticket ID`, `Customer Name`, `Ticket Subject`, `Note Content`, `Note Created`
FROM TICKETS t JOIN
     CUSTOMERS c
     ON t.`Ticket Customer` = c.ID LEFT JOIN
     (SELECT n.*,
             ROW_NUMBER() OVER (PARTITION BY n.`Note Ticket ID` ORDER BY n.`Note Created` DESC) as seqnum
      FROM NOTES n
     ) n
     ON n.`Note Ticket ID` = t.`Ticket ID` AND
        n.seqnum = 1
ORDER BY (n.`Note Created` IS NOT NULL) DESC,
         n.`Note Created` DESC;
1
投票

一种方法是使用子查询,这些子查询在按创建时间降序或创建的最大时间分别排序时获得最上面的一行注释。

SELECT t.`Ticket ID`,
       c.`Customer Name`,
       t.`Ticket Subject`,
       (SELECT n.`Note Content`
               FROM notes n
               WHERE n.`Note Ticket ID` = t.`Ticket ID`
               ORDER BY n.`Note Created` DESC
               LIMIT 1) `Note Content`,
       (SELECT max(n.`Note Created`)
               FROM notes n
               WHERE n.`Note Ticket ID` = t.`Ticket ID`) `Note Created`
       FROM tickets t,
            LEFT JOIN customers c
                      ON c.`Customer ID` = t.`Ticket Customer`;
Popular tags: msyql | oracle | mongodb | postgresql |