Re: Is the selection possible?
Posted in 1997
Yan Liu wrote:
> We have 3 tables, T1, T2, T3.
> T1 has fields a, b1.
> T2 has fields a, b2.
> T3 has fields a, b3.
>
> If b2 is not NULL, I need select T1.a, T2.b2 into $a, $b
> from T1, T2
> where T1.a=T2.a
> ORDER by T2.b2
>
> Otherwise , I need select T1.a, T3.b3 into $a, $b
> from T1, T3
> where T1.a=T3.a
> ORDER by T3.b3
>
I hope I understand this properly. What you might need is a query like:
SELECT T1.a,T2.b2
FROM T1,T2
WHERE T1.a=T2.a AND T2.b2 IS NOT NULL
UNION ALL
SELECT T1.a,T3.b3
FROM T1,T2,T3
WHERE T1.a=T2.a AND T2.a=T3.a AND T2.b2 IS NULL
ORDER BY 1,2
I assume there must be a corresponding row in T2 with a null-value
in b2 for every row that has to be drawn from T3. I have to include
T2 in the 2nd query to be able to check for b2.
Hope this helps,
Roland