假设数据 -
tbl1 -
ID | 日期 | 价值1 |
---|---|---|
101 | 2021-01-01 | 200 |
101 | 2021-01-03 | 400 |
表2-
ID | 日期 | 价值2 |
---|---|---|
101 | 2021-01-01 | 600 |
101 | 2021-01-02 | 900 |
我的预期结果是 -
ID | 日期 | 价值1 | 价值2 |
---|---|---|---|
101 | 2021-01-01 | 200 | 600 |
101 | 2021-01-02 | 钠 | 900 |
101 | 2021-01-03 | 400 | 钠 |
select * from (select * from tbl1 where id in
(another query)) t1
left join tbl2 as t2 on t1.id = t2.id and t1.date = t2.date
union all
select * from (select * from tbl1 where id in
(another query)) t1
right join tbl2 as t2 on t1.id = t2.id and t1.date = t2.date
where t1.id is null and t1.date is null
我无法弄清楚我哪里出错了。