Re: Incorrect Query

From: Sharon Cowling <sharon(dot)cowling(at)sslnz(dot)com>
To: "Joshua b(dot) Jore" <josh(at)greentechnologist(dot)org>
Cc: "Pgsql-Novice (E-mail)" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Incorrect Query
Date: 2002-05-08 20:01:00
Message-ID: 200205082001.g48K1aN27445@lambton.sslnz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

> Why not just use?
>
> WHERE person_id = ''
> OR (firstname = initcap('sharon')
> AND
> lastname = initcap('cowling')
> )
>

Because the user may enter either just the firstname or just the lastname, so the above won't work if I just enter 'cowling' and there are multiple entries of 'cowling' in the database, it returns 0 rows.

select person_id, initcap(firstname), initcap(lastname), dob, street, city
from person5
where person_id = ''
OR (firstname = initcap('')
AND
lastname = initcap('cowling')
);

person_id | initcap | initcap | dob | street | city
-----------+---------+---------+-----+--------+------
(0 rows)

select person_id, initcap(firstname), initcap(lastname), dob, street, city
from person5
where
person_id = ''
or
firstname = initcap('')
or
lastname = initcap('cowling')
or
(
firstname = initcap('sharon')
and
lastname = initcap('')
)

person_id | initcap | initcap | dob | street | city
-----------+---------+---------+------------+---------+------------
1018 | Katrina | Cowling | 07/07/1976 | Long St | Wellington
858 | Sharon | Cowling | 16/10/1979 | A Grove | Wellington

Regards,

Sharon Cowling

> -----Original Message-----
> From: Joshua b. Jore [mailto:josh(at)greentechnologist(dot)org]
> Sent: Thursday, 9 May 2002 01:39
> To: Sharon Cowling
> Cc: Pgsql-Novice (E-mail)
> Subject: Re: [NOVICE] Incorrect Query
>
>
> Sharon,
> You were abusing parentheses and confusing the issue. Don't
> do that, it
> just makes the query less readable.
>
> Here is what you actually wrote:
>
> where
> person_id = ''
> or
> (
> (
> firstname = initcap('sharon')
> )
> or
> (
> lastname = initcap('cowling')
> )
> )
> or
> (
> (
> firstname = initcap('sharon')
> and
> lastname = initcap('cowling')
> )
> )
>
> Which is simplified to (removing parentheses where redundant but
> retaining all the logic). Check out that first bit where you match on
> fname or lname. That last bit doesn't even do anything since the other
> name expressions covered it already.
>
> where
> person_id = ''
> or
> firstname = initcap('sharon')
> or
> lastname = initcap('cowling')
> or
> (
> firstname = initcap('sharon')
> and
> lastname = initcap('cowling')
> )
>
> Why not just use?
>
> WHERE person_id = ''
> OR (firstname = initcap('sharon')
> AND
> lastname = initcap('cowling')
> )
>

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-05-08 20:43:35 Re: Partial table copy?
Previous Message Andre Dubuc 2002-05-08 17:47:25 Re: Appending values non-destructively