我有两个查询,想要在不使用“UNION
”
例如
查询1:
Select
T1.Field1,T1.Field2,T2.Field3
from
T1, T2, T3
where T1.XYZ = T2.XYZ AND T1.STATUS = 'S' AND T1.XYZ = T3.XYZ AND T3.ABC = 'Y';
结果:10行
查询2:
Select
T1.Field1,T1.Field2,T2.Field3
from
T1, T2, T4
where
T1.XYZ = T2.XYZ AND T1.STATUS = 'W' AND T1.XYZ = T4.XYZ AND T4.ABC = 'Y';
结果:5行。
有没有办法在不使用“UNION
”的情况下将查询1和查询2合并为单个,并将结果设为15(10 + 5)。
提前致谢
答案 0 :(得分:2)
只需更改您的查询即可。
*veer*
修改1:
在两个查询中知道一个表不同之后。以下应该工作。我已将逗号更改为select t1.field1,
t1.field2,
t2.field3
from
t1,
t2,
t3
where t1.xyz = t2.xyz
AND (t1.status = 'S' OR t1.status = 'W')
AND t3.abc = 'Y'
。
JOINS
我同意@xQbert,select t1.field1,
t1.field2,
t2.field3
from t1 t1
inner join t2 t2
on t1.xyz = t2.xyz
where t1.status IN ( 'S', 'W' )
不需要t3和t4。即使它增加了数量或记录,但我无法想出在这个特定查询中使用这些表的任何理由。
编辑2:在问题中的新更新之后,以下查询应该有效
CROSS JOIN
答案 1 :(得分:1)
可以写成:
Select T1.Field1,T1.Field2,T2.Field3
from T1, T2, T3, T4
where T1.XYZ = T2.XYZ
AND T1.STATUS IN('W','S')
AND (T4.ABC = 'Y' OR T3.ABC = 'Y')