我想用三个表的联合创建视图。但是在结果查询中,我想要一个额外的列,例如“ tableId”。
我的代码就像
CREATE OR REPLACE VIEW DETAILS AS SELECT * FROM ( SELECT T1.ID, T1.AMOUNT, T1.STATUS, T1.ADDEDBY, T1.ADDEDON FROM Table1 T1 UNION ALL SELECT T2.ID, T2.AMOUNT, T2.STATUS, T2.ADDEDBY, T2.ADDEDON FROM Table2 T2 UNION ALL SELECT T3.ID, T3.BILLAMOUNT, T3.STATUS, T3.ADDEDBY, T3.ADDEDON FROM Table3 T3 );
这给了我所需的三个表的并集。但是如何获取结果输出中的表ID列?该列在三个表中都不存在。
这听起来像你只是想一个额外的硬编码值添加到您的SELECT列表
SELECT
CREATE OR REPLACE VIEW DETAILS AS SELECT * FROM ( SELECT T1.ID, T1.AMOUNT, T1.STATUS, T1.ADDEDBY, T1.ADDEDON, 'T1' tableID FROM Table1 T1 UNION ALL SELECT T2.ID, T2.AMOUNT, T2.STATUS, T2.ADDEDBY, T2.ADDEDON, 'T2' tableID FROM Table2 T2 UNION ALL SELECT T3.ID, T3.BILLAMOUNT, T3.STATUS, T3.ADDEDBY, T3.ADDEDON, 'T3' tableID FROM Table3 T3 );