我一直在处理这两个表:
文档
id company_id etc
=======================
1 2 x
2 2 x
版本
id document_id version date_created date_issued date_accepted
==========================================================================
1 1 1 2013-04-29 2013-04-30 NULL
2 2 1 2013-05-01 NULL NULL
3 1 2 2013-05-01 2013-05-01 2013-05-03
有一个页面我想列出所有文档及其属性。我想从每个文档中添加一个单独的状态。状态可以从对应Version
的 s 拥有的最近日期推导出来。可能正在接受较旧的版本。
我要找的查询结果是这样的:
id company_id etc status
==================================
1 2 x accepted
2 2 x created
我首先进行了一个查询,该查询结合了所有日期并在其旁边添加了一个状态。它按预期工作,当我添加 document_id 时,一切正常。
SELECT `status`
FROM (
SELECT max(date_created) as `date`,'created' as `status` FROM version WHERE document_id = 1
UNION
SELECT max(date_issued),'issued' FROM version WHERE document_id = 1
UNION
SELECT max(date_accepted),'accepted' FROM version WHERE document_id = 1
ORDER BY date DESC
LIMIT 1
) as maxi
当我尝试将此查询合并为子查询时,我无法使其工作。
SELECT *, (
SELECT `status` FROM (
SELECT max(date_created) as `date`,'created' as `status`FROM version WHERE document_id = document.id
UNION
SELECT max(date_issued),'issued' FROM version WHERE document_id = document.id
UNION
SELECT max(date_accepted),'accepted' FROM version WHERE document_id = document.id
ORDER BY date DESC
LIMIT 1
) as maxi
) as `status`
FROM `document`
这会让我出错Unknown column 'document.id' in 'where clause'
。所以我在 SO 上阅读并认为它根本无法达到该值offer.id
,因为它是子查询中的子查询。因此,我尝试采用另一种方法并一次获取所有状态,以避免该WHERE
语句并加入它们。我结束了下一个查询。
SELECT MAX(`date`),`status`, document_id
FROM (
SELECT datetime_created as `date`, 'created' as `status`,document_id FROM `version`
UNION
SELECT datetime_issued, 'issued',document_id FROM `version`
UNION
SELECT datetime_accepted, 'accepted',document_id FROM `version`
) as dates
GROUP BY offer_id
这次没有错误,但我意识到它status
不可能是正确的,因为它在GROUP BY
. 我已经尝试过将两者结合起来,但这两个缺陷一直在阻碍我。任何人都可以建议如何在不更改我的数据库的情况下在单个查询中执行此操作吗?(我知道将日期保存在单独的表格中很简单)