Re: What is the difference between these queries

From: tv(at)fuzzy(dot)cz
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: tv(at)fuzzy(dot)cz, "salah jubeh" <s_jubeh(at)yahoo(dot)com>, "pgsql" <pgsql-general(at)postgresql(dot)org>
Subject: Re: What is the difference between these queries
Date: 2011-04-12 15:12:16
Message-ID: 2123769609e0dae103012303d30956b6.squirrel@sq.gransy.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> tv(at)fuzzy(dot)cz writes:
>>> Query1
>>> -- the first select return 10 rows
>>> SELECT a, b
>>> FROM table1 LEFT JOIN table2 on (table1_id = tabl2_id)
>>> Where table1_id NOT IN (SELECT DISTINCT table1_id FROM table3)
>>> EXCEPT
>>> -- this select return 5 rows
>>> SELECT a, b
>>> FROM table1 LEFT JOIN table2 on (table1_id = tabl2_id)
>>> Where table1_id NOT IN (SELECT DISTINCT table1_id FROM table3)
>>> and b ~* 'pattern'
>>> -- the result is 5 rows
>>>
>>> Query2
>>> --this select return 3 rows
>>> SELECT a, b
>>> FROM table1 LEFT JOIN table2 on (table1_id = tabl2_id)
>>> Where table1_id NOT IN (SELECT DISTINCT table1_id FROM table3)
>>> and b !~* 'pattern'
>>>
>>> Why query1 and query2 return different set. note that query two return
>>> a
>>> subset
>>> of query1
>
>> Those queries obviously are not equivalent - the regular expression is
>> applied to different parts of the query.
>
> Not sure I buy that ... personally I was wondering whether there were
> some null values of b.

Seems you're right - I somehow misread/misunderstood those queries. The
NULL value in 'b' seems like the most probable cause (even the fact that
query2 returns subset of query1 corresponds to this).

regards
Tomas

In response to

Browse pgsql-general by date

  From Date Subject
Next Message akp geek 2011-04-12 16:31:02 word wrap in postgres
Previous Message tomas 2011-04-12 14:56:11 Re: Different cast behavior of TEXT and VARCHAR