Oracle
 sql >> Database >  >> RDS >> Oracle

Come selezionare una colonna da tutte le tabelle in cui risiede?

Per ottenere record da una tabella, devi scrivere una query su quella tabella. Quindi, non puoi ottenere TUTTI i record dalle tabelle con il campo specificato senza una query su ciascuna di queste tabelle.

Se c'è un sottoinsieme di colonne che ti interessa e questo sottoinsieme è condiviso tra tutte le tabelle, puoi utilizzare l'operazione UNION/UNION ALL in questo modo:

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'

Oppure, nel caso più semplice in cui vuoi solo sapere quali tabelle hanno record su un particolare cliente

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'

Altrimenti devi interrogare ogni tabella separatamente.