dbTalk Databases Forums  

query based on result of another query

comp.databases.postgresql comp.databases.postgresql


Discuss query based on result of another query in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael
 
Posts: n/a

Default query based on result of another query - 01-13-2009 , 12:58 PM






Hi,
I'm accessing postgres from php and trying to avoid to much queries to
db. Corently I do one select, then check with php if result is not
empty, if so, then fire another select query.
Is it possible to write 2 selects, so that second uses result of
first? Can I check, if result of first select is empty directly on DB?

something like

@var = select id where id < 10 /* will be not found */

if(!@var) select id where id > 10 /*found*/

Best regards, Michael

Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: query based on result of another query - 01-14-2009 , 03:53 AM






Michael <useitall (AT) gmail (DOT) com> wrote:
Quote:
I'm accessing postgres from php and trying to avoid to much queries to
db. Corently I do one select, then check with php if result is not
empty, if so, then fire another select query.
Is it possible to write 2 selects, so that second uses result of
first? Can I check, if result of first select is empty directly on DB?

something like

@var = select id where id < 10 /* will be not found */

if(!@var) select id where id > 10 /*found*/
SQL describes only what you want to get, it is not a procedural language.

To the best of my knowledge, there is no way to make SQL do what you want.

What can be done is to write one query that contains both queries, like in

SELECT id FROM tab WHERE id < 10
UNION
SELECT id FROM tab WHERE id > 10
AND NOT EXISTS (SELECT 1 FROM tab WHERE id < 10)

but this will cause unnecessary work: there will be at least two and
maybe three scans of the table instead of one or two.

Besides, the query becomes much more complicated.

I'd say that this is acceptable for small and simple queries where the
cost of an additional client-server round trip hurts more than the above,
but if the query is complicated or the tables involved are big, I think
it would be a bad idea.

Yours,
Laurenz Albe


Reply With Quote
  #3  
Old   
Michael
 
Posts: n/a

Default Re: query based on result of another query - 01-14-2009 , 06:36 AM



On Jan 14, 10:53*am, Laurenz Albe <inv... (AT) spam (DOT) to.invalid> wrote:
Quote:
Michael <useit... (AT) gmail (DOT) com> wrote:
I'm accessing postgres from php and trying to avoid to much queries to
db. Corently I do one select, then check with php if result is not
empty, if so, then fire another select query.
Is it possible to write 2 selects, so that second uses result of
first? Can I check, if result of first select is empty directly on DB?

something like

@var = select id where id < 10 /* will be not found */

if(!@var) select id where id > 10 /*found*/

SQL describes only what you want to get, it is not a procedural language.

To the best of my knowledge, there is no way to make SQL do what you want..

What can be done is to write one query that contains both queries, like in

SELECT id FROM tab WHERE id < 10
UNION
SELECT id FROM tab WHERE id > 10
* *AND NOT EXISTS (SELECT 1 FROM tab WHERE id < 10)

but this will cause unnecessary work: there will be at least two and
maybe three scans of the table instead of one or two.

Besides, the query becomes much more complicated.

I'd say that this is acceptable for small and simple queries where the
cost of an additional client-server round trip hurts more than the above,
but if the query is complicated or the tables involved are big, I think
it would be a bad idea.

Yours,
Laurenz Albe
Hi Laurenz,

thank you for reply. I thought DB could do anything for me

Regards, Michael


Reply With Quote
  #4  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: query based on result of another query - 01-14-2009 , 01:00 PM



Michael <useitall (AT) gmail (DOT) com> wrote:

Quote:
I'm accessing postgres from php and trying to avoid to much queries to
db. Corently I do one select, then check with php if result is not
empty, if so, then fire another select query.
Is it possible to write 2 selects, so that second uses result of
first? Can I check, if result of first select is empty directly on DB?

something like

@var = select id where id < 10 /* will be not found */

if(!@var) select id where id > 10 /*found*/
Yes, you can do this using a a function in a procedural language
running inside postgresql.

The simplest way is to use PL/pgSQL; see
http://www.postgresql.org/docs/8.3/s...-overview.html

-M-



Reply With Quote
  #5  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: query based on result of another query - 01-15-2009 , 08:33 AM



Matthew Woodcraft <mattheww (AT) chiark (DOT) greenend.org.uk> wrote:
Quote:
I'm accessing postgres from php and trying to avoid to much queries to
db. Corently I do one select, then check with php if result is not
empty, if so, then fire another select query.
Is it possible to write 2 selects, so that second uses result of
first? Can I check, if result of first select is empty directly on DB?

something like

@var = select id where id < 10 /* will be not found */

if(!@var) select id where id > 10 /*found*/

Yes, you can do this using a a function in a procedural language
running inside postgresql.

The simplest way is to use PL/pgSQL; see
http://www.postgresql.org/docs/8.3/s...-overview.html
True, I completely forgot about that!
I was thinking SQL only.

Yours,
Laurenz Albe


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.