嘿,我在PostgreSql中有两个表:
1 - documents: id, title
2 - updates: id, document_id, date
还有一些数据:
documents:
| 1 | Test Title |
updates:
| 1 | 1 | 2006-01-01 |
| 2 | 1 | 2007-01-01 |
| 3 | 1 | 2008-01-01 |
所以所有的更新都指向同一个文档,但是更新的日期都不同.
我试图做的是从documents表中进行 Select ,但也包括基于日期的最新更新.
这样的查询应该是什么样子?这是我目前拥有的更新,但我列出了所有更新,而不是我需要的最新更新:
SELECT * FROM documents,updates WHERE documents.id=1 AND documents.id=updates.document_id ORDER BY date
包括;我在查询中需要它的原因是我想按更新模板中的日期订购!
编辑:这个脚本非常复杂