表1
Code Name1 type BalanceDue id1 id2 id3 id4 id5 emp
2600 intl-Airfare 1 2.38 120 410 510 603 7060513 null
1100 intl-travel 1 2.66 120 420 540 602 7060513 null
2400 intl-Meals 1 1.50 120 420 520 602 7060513 null
4100 Transpo 2 19.70 110 210 510 601 null
4100 Transpo 2 13.25 110 210 500 601 null
4100 Transpo 2 17.38 110 210 500 600 null
3600 Dom travel 3 25.11 110 210 500 600 55713 null
表2
Code Details Total type code1 code2 code3 code4 code5 emp
4100 no#233 Emp1-Parking 11.39 2 110 210 510 601 null null
4100 no#231 Jes-Parking 6.83 2 110 210 510 601 null null
4100 no#232 Jes-TransExp 1.48 2 110 210 510 601 null null
4100 no#234 Emp2-TollFee 0.23 2 110 210 500 601 null null
4100 no#239 Emp2-Parking 1.82 2 110 210 500 601 null null
4100 no#240 Emp3-Parking 2.96 2 110 210 500 601 null null
4100 no#252 Emp5-TollFee 8.24 2 110 210 500 601 null null
4100 no#235 Jay-TollFee 4.90 2 110 210 500 600 null null
4100 no#243 Jay-TransExp 12.48 2 110 210 500 600 null null
我希望这样做:
如果type为1,则显示表1中具有类型1的所有值
就像类型是3
一样
如果type为2,则显示表2中的所有值,考虑到code1到code5
结果
Code Details type Total code1 code2 code3 code4 code5 emp
2600 intl-Airfare 1 2.38 120 410 510 603 7060513 null
1100 intl-travel 1 2.66 120 420 540 602 7060513 null
2400 intl-Meals 1 1.50 120 420 520 602 7060513 null
4100 no#233 Emp1-Parking 2 11.39 110 210 510 601 null null
4100 no#231 Jes-Parking 2 6.83 110 210 510 601 null null
4100 no#232 Jes-TransExp 2 1.48 110 210 510 601 null null
4100 no#234 Emp2-TollFee 2 0.23 110 210 500 601 null null
4100 no#239 Emp2-Parking 2 1.82 110 210 500 601 null null
4100 no#240 Emp3-Parking 2 2.96 110 210 500 601 null null
4100 no#252 Emp5-TollFee 2 8.24 110 210 500 601 null null
4100 no#235 Jay-TollFee 2 4.90 110 210 500 600 null null
4100 no#243 Jay-TransExp 2 12.48 110 210 500 600 null null
3600 Dom travel 3 25.11 110 210 500 600 55713 null
有时,id5有一个值,有时其他列为null。 对不起,我希望我能清楚地解释清楚:( 谢谢大家!
答案 0 :(得分:0)
您期望的结果看起来像UNION
(UNION
子句允许您在单个最终结果集中“合并”更多查询),类似于:
SELECT Code, Name1 as Details, type, BalanceDue as Total,id1 as code1, id2 as code2,
id3 as code3, id4 as code4, id5 as code5, emp
FROM Table1 WHERE type IN (1, 3) -- could also be WHERE type = 1 OR type = 3
UNION
SELECT Code, Details, type, Total, code1, code2, code3, code4, code5, emp
FROM Table2 WHERE type = 2