Обсуждение: 3 SELECTs rolled into 1 ?

Поиск
Список
Период
Сортировка

3 SELECTs rolled into 1 ?

От
Tarlika Elisabeth Schmitz
Дата:
I have 3 similar SELECTs. I am wondering whether they could be rolled
into one?


SELECTitem.id department.id || section.id || category.id as x
FROM itemLEFT JOIN product ON ...LEFT JOIN department ON ...LEFT JOIN section ON ...LEFT JOIN category ON ...

SELECTitem.id department.id || section.id as x
FROM itemLEFT JOIN product ON ...LEFT JOIN department ON ...LEFT JOIN section ON ...

SELECTitem.id department.id as x
FROM itemLEFT JOIN product ON ...LEFT JOIN department ON ...



--


Best Regards,

Tarlika Elisabeth Schmitz


A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad? 


Re: 3 SELECTs rolled into 1 ?

От
"A. Kretschmer"
Дата:
am  Wed, dem 04.06.2008, um  8:41:29 +0100 mailte Tarlika Elisabeth Schmitz folgendes:
> I have 3 similar SELECTs. I am wondering whether they could be rolled
> into one?
> 
> 
> SELECT
>  item.id 
>  department.id || section.id || category.id as x

Syntax error, missing , between the columns ;-)



> FROM item
>  LEFT JOIN product ON ...
>  LEFT JOIN department ON ...
>  LEFT JOIN section ON ...
>  LEFT JOIN category ON ...
> 
> SELECT
>  item.id 
>  department.id || section.id as x
> FROM item
>  LEFT JOIN product ON ...
>  LEFT JOIN department ON ...
>  LEFT JOIN section ON ...
> 
> SELECT
>  item.id 
>  department.id as x
> FROM item
>  LEFT JOIN product ON ...
>  LEFT JOIN department ON ...


If i understand you correctly: use UNION, like 

select foo1 as foo, bar1 as bar from table1 UNION ALL select foo2, bar2 from table2;

The result table contains 2 columns foo and bar and all rows from both
selects.

Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net


Re: 3 SELECTs rolled into 1 ?

От
Tarlika Elisabeth Schmitz
Дата:
On Wed, 4 Jun 2008 09:55:46 +0200
"A. Kretschmer" <andreas.kretschmer@schollglas.com> wrote:

> am  Wed, dem 04.06.2008, um  8:41:29 +0100 mailte Tarlika Elisabeth
> Schmitz folgendes:
> > I have 3 similar SELECTs. I am wondering whether they could be
> > rolled into one?
> > 
> > 
> > SELECT
> >  item.id,
> >  department.id || section.id || category.id as x
> > FROM item
> >  LEFT JOIN product ON ...
> >  LEFT JOIN department ON ...
> >  LEFT JOIN section ON ...
> >  LEFT JOIN category ON ...
> > 
> > ...
> > 
> > SELECT
> >  item.id, 
> >  department.id as x
> > FROM item
> >  LEFT JOIN product ON ...
> >  LEFT JOIN department ON ...
> 
> 
> If i understand you correctly: use UNION, like 
> 
> select foo1 as foo, bar1 as bar from table1 UNION ALL select foo2,
> bar2 from table2;
> 
> The result table contains 2 columns foo and bar and all rows from both
> selects.

Many thanks!
Yes, this produces the desired result.

It is more or less just a concatenation of virtually identical SELECTs
with the same long WHERE clause. I had hoped to be able to express it
with a single SELECT. ;-)

--


Best Regards,
Tarlika Elisabeth Schmitz


A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad?