待查表一待查表二
可以看到表二比表一多了三个字段,其余完全相同,现在想要把两张表中的所有结果查出来,可以使用union,没有的字段可以使用null as;
SELECT
`id`,
`index_name`,
`operator`,
`index_type`,
`is_open`,
`operate_time`,
`is_del`,
NULL AS `father_id`,
NULL AS `score`,
NULL AS `reference_value`
FROM
table1
UNION
SELECT
`id`,
`father_id`,
`index_name`,
`score`,
`reference_value`,
`operator`,
`index_type`,
`is_open`,
`operate_time`,
`is_del`
FROM
table2
查询结果:
可以看到字段并没有对应上,时间的数据显示在了sorce字段中,所有查询的字段需要一一对应。
修改后的sql:
SELECT
`id`,
`index_name`,
`operator`,
`index_type`,
`is_open`,
`operate_time`,
`is_del`,
NULL AS `father_id`,
NULL AS `score`,
NULL AS `reference_value`
FROM
BUILD1_EVALUATION_INDEX
UNION
SELECT
`id`,
`index_name`,
`operator`,
`index_type`,
`is_open`,
`operate_time`,
`is_del`,
`father_id`,
`score`,
`reference_value`
FROM
BUILD2_EVALUATION_INDEX