您好,欢迎访问一九零五行业门户网

mysql 全连接查询合并字段

1、创建两张表 t1如下: id name 1 a1 3 a3 t2如下: id name 1 b1 2 b2 5 b5 联合查询合并字段: sql语句: select t3.`name` as b, t3.`name1` as c ,coalesce(t3.id2,t3.id1) as myid from (select t1.id as id2 ,t1.`name` as name,t2.id as id1 ,t2.`na
1、创建两张表
t1如下:
id name
1 a1
3 a3
t2如下:
id name
1 b1
2 b2
5 b5
联合查询合并字段:
sql语句:
select t3.`name` as b, t3.`name1` as c ,coalesce(t3.id2,t3.id1)  as myid
from (select t1.id as id2 ,t1.`name` as name,t2.id as id1 ,t2.`name` as name1 from t1 left join t2 on t1.id=t2.id 
union select t1.id as id2 ,t1.`name` as name,t2.id as id1 ,t2.`name` as name1  from  t1 right join t2 on t1.id=t2.id) as t3 group by  myid;
查询结果:
b c myid
a1 b1 1
  b2 2
a3   3
  b5 5
其它类似信息

推荐信息