PostgreSQL , Select from 2 tables, but only the latest element from table 2

后端 未结 5 2072
星月不相逢
星月不相逢 2021-02-07 07:07

Hey, I have 2 tables in PostgreSql:

1 - documents: id, title
2 - updates: id, document_id, date

and some data:

documents:

5条回答
  •  星月不相逢
    2021-02-07 07:45

    From the top of my head:

    ORDER BY date DESC LIMIT 1
    

    If you really want only id 1 your can use this query:

    SELECT * FROM documents,updates 
        WHERE documents.id=1 AND updates.document_id=1 
        ORDER BY date DESC LIMIT 1
    

    http://www.postgresql.org/docs/8.4/interactive/queries-limit.html

提交回复
热议问题