您的位置:首页 > 数据库

SQL 列不同的表查询结果合并

2017-09-20 11:10 309 查看
两个不同的表进行查询,需要把结果合并,

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

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

table1:



table2:



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

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;

结果为:



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



type_id,pro_id补充到table2中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: