You have two (or more) queries returning the same fields and want to merge their output.
Use the UNION keyword to join your two SELECT statements ref.
To see a real-world example of this run "psql -E" and type "\d" to see tables.
Let's assume we add a diary_archive table and move entries from diary into it once they are older than 6 months. Sooner or later you are going to want to see all the diary entries for a company both current and archived, so:
SELECT * FROM diary WHERE dy_company=1 UNION SELECT * FROM diary_archive WHERE dy_company=1 ORDER BY dy_id; |
Note that we only need one ORDER BY to sort the output of the combined query. Also, the fields in this case were the same, but this doesn't need to be the case. The important thing is that the types match - if the field1 from the first select is an int8 field1 from the second select should be too.