Data from two tables into one view

前端 未结 2 1080
温柔的废话
温柔的废话 2021-02-12 15:12

Is it possible to grab data from two tables (that have the same fields) into one view. Basically, so the view sees the data as if it was one table.

2条回答
  •  野的像风
    2021-02-12 15:43

    create or replace view view_name as
    select * from table_1
    union all select * from table_2
    

    Note: The columns in the view are set at the time the view is created. Adding columns to table_1 and table_2 after view creation will not show up in view_name. You will need to re-run the above DDL to get new columns to show up.

    If you want duplicate rows to be collasped to single rows (but potentially more work for the server):

    create or replace view view_name as
    select * from table_1
    union select * from table_2
    

    Generally it is bad form to use * in the select list, but assuming that the queries using the view are going to choose just what they need, I would use it here instead of explicitily naming all the columns. (Especially since I wouldn't want to have to add the column names when table_1 and table_2 change.)

提交回复
热议问题