Subject | Re: Sql query question |
---|---|
Author | jasajona |
Post date | 2004-11-17T10:05:29Z |
> As a general thing, one prefers to write queries with joins ratherBut if I have something like this:
> than subselects. It's more elegant and gives the query optimizer
> something to think about.
>
> select t1.f1, t2.f1, t1.f3
> from table1 t1
> left outer join table2 t2 (on t2.f3=t1.f1)
> where t1.f4 = 6
>
> If you don't want rows from t1 that don't have matching rows
> in t2, leave out the "left outer" and give the query optimizer
> something more to work on.
select field1,
(select sum(table2.field1) from table2 where field3=table1.field1
group by table2.field3),
(select sum(table3.field1) from table3 where
field3=table1.field1group by table3.field3),
field3
from table1
where field4=6
probably there are no other ways to write more optimised, am I wrong?