【问题标题】:How do I select the last child row of a parent/child relationship using SQL如何使用 SQL 选择父/子关系的最后一个子行
【发布时间】:2009-12-17 22:58:47
【问题描述】:

仅使用 SQL (MySQL) 我想选择父子关系的每个最后子行,其中子行按时间戳排序。

例如使用表invoicesinvoice_items,我希望分别为每个invoice 提供最新的(即:最近时间戳)invoice_items 记录。

--------------------------
|Invoices                |
--------------------------
|invoice_id| other fields|
--------------------------
| 1        | ...         |
--------------------------
| 2        | ...         |
--------------------------
| 3        | ...         |
--------------------------

--------------------------------------------
|Invoice_Items                             |
--------------------------------------------
| id | invoice_id | invoice_item_timestamp |
--------------------------------------------
| 1  | 1            | 2009-12-01 10:00:00  |
--------------------------------------------
| 2  | 1            | 2009-12-01 10:01:00  |
--------------------------------------------
| 3  | 1            | 2009-12-01 10:02:00  |
--------------------------------------------
| 4  | 2            | 2009-12-01 9:00:00   |
--------------------------------------------
| 5  | 3            | 2009-12-02 08:30:00  |
--------------------------------------------
| 6  | 3            | 2009-12-03 08:31:00  |
--------------------------------------------    

生成类似于下表的结果集的最佳 SQL 语法是什么?

-----------------------------------------------------
|invoice_id| invoice_item_id |invoice_item_timestamp|
---------------------------------------------------
| 1        | 3               | 2009-12-01 10:02:00  |
| 2        | 4               | 2009-12-01 09:00:00  |
| 3        | 6               | 2009-12-03 08:31:00  |
-----------------------------------------------------

【问题讨论】:

    标签: sql mysql


    【解决方案1】:
    SELECT i.*,it.* 
    FROM invoices i
    INNER JOIN (
     SELECT invoice_id, MAX(invoice_item_timestamp) 
     FROM invoice_items
     GROUP BY invoice_id
    ) it ON (i.invoice_id=it.invoice_id)
    

    【讨论】:

      【解决方案2】:

      我不使用 MySQL,但你会明白的:

      select 
        invoices in
      
        inner join invoice_items it
        on in.invoice_id = it.invoice_id
        and invoice_item.invoice_item_id =
           (select max(invoice_item_id) from invoice_item 
              where invoice_id = in.invoice_id)
      

      ...假设 invoice_item 表中没有 invoice_item_id 和 invoice_id 重复项。

      【讨论】:

        【解决方案3】:

        我认为这样的事情会起作用:

        select i.invoice_id, ii.id, ii.invoice_item_timestamp from invoices i left join invoice_items ii on ii.invoice_id = i.invoice_id
         where ii.invoice_item_timestamp = 
           (select max(ii2.invoice_item_timestamp) from invoice_items ii2 where ii2.invoice_id = ii.invoice_id)
        

        【讨论】:

          【解决方案4】:

          你可以试试这个(未经测试):

          SELECT a.*
          FROM Invoices a, invoice_items b
          WHERE a.invoice_id = b.invoice_id 
          ORDER BY invoice_item_timestamp DESC
          LIMIT 1
          

          【讨论】:

          • 这不起作用。 LIMIT 子句使记录集只返回一条记录。在您提供的示例中,它将返回具有最高 PersonID 的一条记录。我正在寻找所有父母的最后一个孩子记录。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多