MySQL获取依赖最新插入的最新日期

时间:2014-03-25 22:35:47

标签: php mysql sql

我有两个查询我正在尝试连接在一起所以我不必一个接一个地执行,因为在某些情况下我想要更改输出顺序。

第一个查询,获取发票状态。它会更改为保留日志,但99%的时间,我只需要检查最新条目。布局如下:

  

providers_invoice_status_ID,providers_invoice_summary_ID,status_ID,   userinfo_ID,providers_invoice_status_date

SELECT providers_invoice_summary_ID
  FROM db_providers_invoice_status
 WHERE status_ID = '$status'
 ORDER BY providers_invoice_status_date DESC

然后根据上述查询的结果查询发票表。它的布局如下。

  

providers_invoice_summary_ID,providers_invoice_summary_file,providers_invoice_summary_total,providers_invoice_summary_due,providers_invoice_summary_generated

    SELECT si.providers_invoice_summary_ID
     , providers_invoice_summary_file
     , providers_invoice_summary_total
     , providers_invoice_summary_due
     , providers_invoice_summary_generated
  FROM db_providers_invoice_summary si 
 WHERE providers_invoice_summary_ID = '$invoice_ID';

可能会尝试合并查询,结果如下:

SELECT si.providers_invoice_summary_ID
     , providers_invoice_summary_file
     , providers_invoice_summary_total
     , providers_invoice_summary_due
     , providers_invoice_summary_generated
     , s.status_ID 
  FROM db_providers_invoice_summary si, db_providers_invoice_status s
 WHERE status_ID = ( SELECT status_ID
                   FROM db_providers_invoice_status
                  WHERE providers_invoice_summary_ID = si.providers_invoice_summary_ID 
                    AND status_ID = 7  
                  ORDER 
                     BY providers_invoice_status_date DESC 
                  LIMIT 1)

但是,它从state_ID = 7的状态记录表中提取所有结果,因此不能获得最新的插入(状态可能已经变为8,因此不需要该invoice_ID)。我试过使用ORDER BY但它只对所有结果进行排序,而不是我所追求的子集。

对此的任何帮助都将非常感激。希望对于阅读它的人来说这是相当清楚的,我知道它非常复杂。

编辑数据:

发票表:

providers_invoice_summary_ID providers_invoice_summary_file providers_invoice_summary_total providers_invoice_summary_due providers_invoice_summary_generated
----------------------------------------------------------------------------------------------------------------------------------------------------------
           4                |        ../blah/blah.jpg      |            245.63       |           2014-04-20               |           2014-03-14
           5                |        ../blah/blah.jpg      |            456.89       |           2014-04-20               |           2014-03-12
           6                |        ../blah/blah.jpg      |            125.36       |           2014-04-24               |           2014-03-12    

状态表:

providers_invoice_status_ID providers_invoice_summary_ID status_ID userinfo_ID providers_invoice_Status_date
------------------------------------------------------------------------------------------------------------
               4           |              4             |   7     |      7    |   2014-03-14 10:19:41
               5           |              5             |   7     |      7    |   2014-03-12 10:22:41
               6           |              6             |   7     |      7    |   2014-03-24 10:15:38
               7           |              5             |   8     |      7    |   2014-03-26 11:15:14

我希望从这些数据中获得2张发票,因为两张发票目前的最新状态设置为7.希望这可以进一步澄清事情。

1 个答案:

答案 0 :(得分:0)

这是两个查询的组合版本。我创建了一个子查询来选择状态与输入状态ID匹配的发票(例如7)。反过来,状态ID是使用最新状态日期从子查询中获取的。

更新的查询

SELECT 
 si.providers_invoice_summary_ID
 , si.providers_invoice_summary_file
 , si.providers_invoice_summary_total
 , si.providers_invoice_summary_due
 , si.providers_invoice_summary_generated
 , invoice_status.status_ID 
FROM 
  db_providers_invoice_summary si, 
  (SELECT t1.providers_invoice_summary_ID as providers_invoice_summary_ID
      , t1.status_id as status_id
   FROM db_providers_invoice_status t1
  LEFT JOIN db_providers_invoice_status t2 ON t1.providers_invoice_summary_ID = t2.providers_invoice_summary_ID AND t1.providers_invoice_Status_date < t2.providers_invoice_Status_date
WHERE t2.providers_invoice_summary_ID IS NULL
  AND t1.status_id = 7
   ) invoice_status
WHERE SI.PROVIDERS_INVOICE_SUMMARY_ID = invoice_status.PROVIDERS_INVOICE_SUMMARY_ID

请查看sql fiddle以获取演示。

<强>参考

MySQL: The Rows Holding the Group-wise Maximum of a Certain Column