SQL如何合并多个查询结果

如题所述

1.两个不同的表进行查询,需要把结果合并,

比如table1的列为 id, user_id, type_id,pro_id;

table2的列为 id,user_id,collect_id;分别如下图所示

table1:

table2:

2.将两个表的查询结果合并到一起的查询语句为

select *, null as collect_id from table1 where user_id = 527

union

select id,user_id,null as type_id,null as pro_id, collect_id from table2 where user_id = 527;

3.结果为:

总结:其实就是把对应的列补充到没有该列的表中,在例子中就是把collect_id补充到table1中,

把type_id,pro_id补充到table2中。

温馨提示:答案为网友推荐,仅供参考
相似回答