PostgreSQL: How to union 3 tables sorted by date

前端 未结 2 344
小鲜肉
小鲜肉 2020-12-31 09:25

I have 3 different SQL queries from 3 different unrelated tables (all using LIMIT and ORDER BY).

I would like to merge and sort the results according to the \"date\"

2条回答
  •  醉梦人生
    2020-12-31 09:40

    SELECT.....
     UNION ALL 
    SELECT....
     UNION ALL
    SELECT ...
     ORDER BY date_field;
    

    For the best performance, apply ORDER BY / LIMIT as late as possible, and avoid it in subqueries.

提交回复
热议问题