How to select a column from all tables in which it resides?

前端 未结 3 1827
北海茫月
北海茫月 2021-01-07 01:58

I have many tables that have the same column \'customer_number\'. I can get a list of all these table by query:

SELECT table_name FROM ALL_TAB_COLUMNS 
WHERE         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 02:15

    To get record from a table, you have write a query against that table. So, you can't get ALL the records from tables with specified field without a query against each one of these tables.

    If there is a subset of columns that you are interested in and this subset is shared among all tables, you may use UNION/UNION ALL operation like this:

    select * from (
    select customer_number, phone, address from table1
    union all
    select customer_number, phone, address from table2
    union all
    select customer_number, phone, address from table3
    )
    where customer_number = 'my number'
    

    Or, in simple case where you just want to know what tables have records about particular client

    select * from (
    select 'table1' src_tbl, customer_number from table1
    union all
    select 'table2', customer_number from table2
    union all
    select 'table3', customer_number from table3
    )
    where customer_number = 'my number'
    

    Otherwise you have to query each table separatelly.

提交回复
热议问题