我有三个具有以下结构的表:
sequence
cli
datetime
company
表格为orders_broadband
orders_lines
和orders_porting
我想显示同一结果集中所有三个表的结果。
我试过运行查询:
SELECT orders_broadband.*, orders_lines.*, orders_porting.* from orders_broadband, orders_lines, orders_porting
但我不认为我的查询正确
答案 0 :(得分:2)
你可以这样做:
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'