SQL 与条件字段联合
SQL Union with Conditional Field
我有以下(简化)tables:
No0 | No1 | start0 | end0
10000 | | |
19999 | | 10000 | 19999
20000 | | |
29999 | | 20000 | 29999
No0 | No1 | start0 | end0
| 11000 | |
| 15950 | | 15950
| 16000 | |
我如何UNION
他们(或其他方法),但根据No1
介于[=16之间] table 2日填写适当的No0
=] 和 end0
这样 returns:
No0 | No1 | start0 | end0
10000 | | |
19999 | | 10000 | 19999
20000 | | |
29999 | | 20000 | 29999
19999 | 11000 | |
19999 | 15950 | |
19999 | 16000 | |
在并集的下半部分,返回到 TableA,其中 No1 在 Start0 和 End0 之间。
举个例子:
select ta.No0,
ta.No1,
ta.Start0,
ta.End0
from TableA as ta
union all
select ta.No0,
tb.No1,
NULL as Start0,
NULL as End0
from TableB as tb
inner join TableA as ta on tb.No1 between ta.Start0 and ta.End0;
我有以下(简化)tables:
No0 | No1 | start0 | end0
10000 | | |
19999 | | 10000 | 19999
20000 | | |
29999 | | 20000 | 29999
No0 | No1 | start0 | end0
| 11000 | |
| 15950 | | 15950
| 16000 | |
我如何UNION
他们(或其他方法),但根据No1
介于[=16之间] table 2日填写适当的No0
=] 和 end0
这样 returns:
No0 | No1 | start0 | end0
10000 | | |
19999 | | 10000 | 19999
20000 | | |
29999 | | 20000 | 29999
19999 | 11000 | |
19999 | 15950 | |
19999 | 16000 | |
在并集的下半部分,返回到 TableA,其中 No1 在 Start0 和 End0 之间。
举个例子:
select ta.No0,
ta.No1,
ta.Start0,
ta.End0
from TableA as ta
union all
select ta.No0,
tb.No1,
NULL as Start0,
NULL as End0
from TableB as tb
inner join TableA as ta on tb.No1 between ta.Start0 and ta.End0;