[英]SQL selecting from 3 tables with the same column names
我有三個具有以下結構的表:
sequence
cli
datetime
company
該表是orders_broadband
orders_lines
和orders_porting
我想顯示同一結果集中所有三個表的結果。
我試過運行查詢:
SELECT orders_broadband.*, orders_lines.*, orders_porting.* from orders_broadband, orders_lines, orders_porting
但我不認為查詢正確
你可以這樣做:
select sequence, cli, datetime, company from orders_broadband
union all
select sequence, cli, datetime, company from orders_lines
union all
select sequence, cli, datetime, company from orders_porting
這將合並所有3個表中的數據。
通過顯式添加列名進行編輯(類似於注釋部分中提到的xQbert)
顯示表名
select sequence, cli, datetime, company, 'Tablename1' as tbl from orders_broadband where status = 'New'
UNION ALL
select sequence, cli, datetime, company, 'Tablename2' as tbl from orders_lines where status = 'New'
UNION ALL
select sequence, cli, datetime, company, 'Tablename3' as tbl from orders_porting where status = 'New'
將“ Tablename1”和其他名稱更改為您認為合適的名稱。
如果要從orders_broadband添加其他列怎么辦?
select '' as telephone_number, sequence, cli, datetime, company, 'Tablename1' as tbl from orders_broadband where status = 'New'
UNION ALL
select '' as telephone_number, sequence, cli, datetime, company, 'Tablename2' as tbl from orders_lines where status = 'New'
UNION ALL
select telephone_number, sequence, cli, datetime, company, 'Tablename3' as tbl from orders_porting where status = 'New'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.