来自2个具有空值的表的SQL联接
我有两个表,想把结果从表中拉回到一个表中。 现在,名称字段是一个唯一的ID,它附带了多个数据,即日期和时间。我已经将数据简化到这里,但这是一般要点 表1来自2个具有空值的表的SQL联接,sql,sql-server,Sql,Sql Server,我有两个表,想把结果从表中拉回到一个表中。 现在,名称字段是一个唯一的ID,它附带了多个数据,即日期和时间。我已经将数据简化到这里,但这是一般要点 表1 Name Date John 12th John 13th John 15th John 17th 表2 Name Colour John Red John Blue John Orange John Green 需要的结果 Name Date Time John
Name Date
John 12th
John 13th
John 15th
John 17th
表2
Name Colour
John Red
John Blue
John Orange
John Green
需要的结果
Name Date Time
John 12th NULL
John 13th NULL
John 15th NULL
John 17th NULL
John NULL Red
John NULL Blue
John NULL Orange
John NULL Green
我目前正在执行一个左连接来拉取数据,但是它会将结果彼此贴在一起,就像
John 12th Red
您想要
全部联合:
select name, date, null as colour
from t1
union all
select name, null, colour
from t2;
我冒昧地给第二列命名为颜色
,而不是时间
,因为这在问题的上下文中更有意义。您想要联合所有人
:
select name, date, null as colour
from t1
union all
select name, null, colour
from t2;
我冒昧地给第二栏命名为颜色
,而不是时间
,因为这在问题的上下文中更有意义。用你正在使用的数据库标记你的问题。向我们展示你正在使用的查询(你的问题-不要在评论中发布代码或其他信息)你的问题和预期输出对我来说没有任何意义,我认为单表数据表示为多表。用你正在使用的数据库标记你的问题。向我们展示你正在使用的查询(你的问题-不要在注释中发布代码或其他信息)你的问题和预期输出对我来说没有任何意义,我认为单表数据表示为多表。