Re: [SQL] exists and is not null equivalence in query

2009-05-28 Thread Tom Lane
Raphael Bauduin writes: > select count(t.trame_id) as count, v.voiture_num as voitureNum from > arch_trames t left join voiture v on (v.tag_id=t.tag_id) > where > (t.recept_time >= 1243509320691) > and exists (select v2.voiture_num from voiture v2 where v2.tag_id=v.tag_id) > group by v.voiture_

[SQL] exists and is not null equivalence in query

2009-05-28 Thread Raphael Bauduin
Hi, In some code I am taking over, I found this query: select count(t.trame_id) as count, v.voiture_num as voitureNum from arch_trames t left join voiture v on (v.tag_id=t.tag_id) where (t.recept_time >= 1243509320691) and exists (select v2.voiture_num from voiture v2 where v2.tag_id=v.tag_id)

Re: [SQL] EXISTS

2008-10-14 Thread Denis Woodbury
Thanks to those who responded, I see PL/PgSQL is the way to go Regards, denis woodbury on 10/11/08 1:32 PM, [NAME] at [ADDRESS] wrote: > Denis Woodbury <[EMAIL PROTECTED]> writes: >> I would like to know if this this type of statement can be used in >> Postgresql > >> IF NOT EXISTS (SELECT 1

Re: [SQL] EXISTS

2008-10-14 Thread Craig Ringer
Denis Woodbury wrote: Hi, I would like to know if this this type of statement can be used in Postgresql IF NOT EXISTS (SELECT 1 FROM Table WHERE col1 = 'mystring' ) BEGIN ... END PostgreSQL doesn't have any sort of free block flow control; it doesn't have an IF statement or similar in SQL. Y

Re: [SQL] EXISTS

2008-10-11 Thread Tom Lane
Denis Woodbury <[EMAIL PROTECTED]> writes: > I would like to know if this this type of statement can be used in > Postgresql > IF NOT EXISTS (SELECT 1 FROM Table WHERE col1 = 'mystring' ) > BEGIN > ... > END I suspect you are trying to type that directly into SQL. You need to be using plpgsql in

Re: [SQL] EXISTS

2008-10-11 Thread Leif B. Kristensen
On Saturday 11. October 2008, Denis Woodbury wrote: >Hi, > >I would like to know if this this type of statement can be used in >Postgresql > >IF NOT EXISTS (SELECT 1 FROM Table WHERE col1 = 'mystring' ) >BEGIN >... >END > >If it can, any idea why I get this error. >** Error ** > >ER

[SQL] EXISTS

2008-10-11 Thread Denis Woodbury
Hi, I would like to know if this this type of statement can be used in Postgresql IF NOT EXISTS (SELECT 1 FROM Table WHERE col1 = 'mystring' ) BEGIN ... END If it can, any idea why I get this error. ** Error ** ERROR: syntax error at or near "IF" SQL state: 42601 Character: 2

Re: [SQL] EXISTS Keyword

2001-10-03 Thread Bruce Momjian
> Hello! > > I vaguely remember that someone said somewhere that > EXISTS runs faster than IN: > > SELECT * FROM table1 where field1 EXISTS (SELECT > field4 FROM table2) Move field1 into the subquery and join it to table2. See the FAQ for an example. -- Bruce Momjian

[SQL] EXISTS Keyword

2001-10-03 Thread cnliou
Hello! I vaguely remember that someone said somewhere that EXISTS runs faster than IN: SELECT * FROM table1 where field1 EXISTS (SELECT field4 FROM table2) However, all I got from version 7.1.3 is: ERROR: parser: parse error at or near "exists" While below works: SELECT * FROM table1 where

Re: [SQL] exists

2001-08-21 Thread Joseph Shraibman
I'm running 7.1.3. What does 'rows=1' mean? The number of rows returned or the number postgres has to look through? Tom Lane wrote: > Joseph Shraibman <[EMAIL PROTECTED]> writes: > >>Why does explain show more than one row, even if there is a LIMIT = 1? >> > > What version are you runni

Re: [SQL] exists

2001-08-21 Thread Tom Lane
Joseph Shraibman <[EMAIL PROTECTED]> writes: > Why does explain show more than one row, even if there is a LIMIT = 1? What version are you running? I get results like regression=# explain select * from tenk1 limit 1; NOTICE: QUERY PLAN: Limit (cost=0.00..0.03 rows=1 width=148) -> Seq Scan

Re: [SQL] exists

2001-08-21 Thread Joseph Shraibman
Why does explain show more than one row, even if there is a LIMIT = 1? -- Joseph Shraibman [EMAIL PROTECTED] Increase signal to noise ratio. http://www.targabot.com ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAI

Re: [SQL] exists

2001-08-21 Thread Tom Lane
Joseph Shraibman <[EMAIL PROTECTED]> writes: > Then why does the explain say rows=1363 ? That's the estimate of how many rows the inner SELECT would return, if left free to return them all. You should get the same row count estimate (though quite possibly a different plan) if you just do an EXPL

Re: [SQL] exists

2001-08-21 Thread Joseph Shraibman
Then why does the explain say rows=1363 ? I don't mean to nitpick here, but maybe this is the symptom of a larger problem. Tom Lane wrote: > Joseph Shraibman <[EMAIL PROTECTED]> writes: > >>Well the total cost should be at least as big as the sub-costs, no? >> > > Not if the sub-plan in questi

Re: [SQL] exists

2001-08-21 Thread Tom Lane
Joseph Shraibman <[EMAIL PROTECTED]> writes: > Well the total cost should be at least as big as the sub-costs, no? Not if the sub-plan in question is for an EXISTS. The sub-plan cost is stated in terms of cost to retrieve all rows --- but the outer level EXISTS isn't going to retrieve all rows,

Re: [SQL] exists

2001-08-21 Thread Stephan Szabo
> Stephan Szabo wrote: > > On Tue, 21 Aug 2001, Joseph Shraibman wrote: > > > > > >>Thank you, I was missing the parens. > >> > >>If I do an explain I see: > >> > >>-> Index Scan using m_u_and_p_key on m (cost=0.00..3035.22 rows=1363 width=12) > >> > >> > >>even if I put a limit 1 on the sele

Re: [SQL] exists

2001-08-21 Thread Joseph Shraibman
Stephan Szabo wrote: > On Tue, 21 Aug 2001, Joseph Shraibman wrote: > > >>Thank you, I was missing the parens. >> >>If I do an explain I see: >> >>-> Index Scan using m_u_and_p_key on m (cost=0.00..3035.22 rows=1363 width=12) >> >> >>even if I put a limit 1 on the select. Why is that? >> >

Re: [SQL] exists

2001-08-21 Thread Joseph Shraibman
Stephan Szabo wrote: >>Limit (cost=48.39..48.39 rows=1 width=70) >> -> Sort (cost=48.39..48.39 rows=2 width=70) >> -> Hash Join (cost=18.46..48.38 rows=2 width=70) >> -> Index Scan using u_p_key on u (cost=0.00..27.66 rows=48 width=28) >> -> Hash (c

Re: [SQL] exists

2001-08-21 Thread Stephan Szabo
On Tue, 21 Aug 2001, Joseph Shraibman wrote: > Thank you, I was missing the parens. > > If I do an explain I see: > > -> Index Scan using m_u_and_p_key on m (cost=0.00..3035.22 rows=1363 width=12) > > > even if I put a limit 1 on the select. Why is that? Is that the inner query (on the ex

Re: [SQL] exists

2001-08-21 Thread Joseph Shraibman
Thank you, I was missing the parens. If I do an explain I see: -> Index Scan using m_u_and_p_key on m (cost=0.00..3035.22 rows=1363 width=12) even if I put a limit 1 on the select. Why is that? Stephan Szabo wrote: > On Mon, 20 Aug 2001, Joseph Shraibman wrote: > > >>I want to select a b

Re: [SQL] exists

2001-08-20 Thread Stephan Szabo
On Mon, 20 Aug 2001, Joseph Shraibman wrote: > I want to select a boolean if there exists a row in another table that matches this >one. > So I did select ..., (select count(*) from table2 where ...) > 0 ... > but that count(*) was taking forever. I know there is a better way to do it, but >

[SQL] exists

2001-08-20 Thread Joseph Shraibman
I want to select a boolean if there exists a row in another table that matches this one. So I did select ..., (select count(*) from table2 where ...) > 0 ... but that count(*) was taking forever. I know there is a better way to do it, but whenever I try to use EXISTS I get a syntax error. Wh