牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2017-09-20 投稿人: 浏览次数:2107

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

比如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中。


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。