Re: Need efficient way to do comparison with NULL as an option

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: "D(dot) Dante Lorenso" <dante(at)lorenso(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general(at)postgresql(dot)org
Subject: Re: Need efficient way to do comparison with NULL as an option
Date: 2008-01-05 05:39:33
Message-ID: 477F1815.8070502@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

D. Dante Lorenso wrote:

> But if I want to find all the items which are not in any folder, I want:
>
> SELECT *
> FROM mytable
> WHERE folder_id IS NULL;
>
> I don't have any folder_id 0, so on a URL I might do this:
>
> http://xyz/page.php?fid=123
> http://xyz/page.php?fid=0

Why not just have fid undef? thus you can just say:

>
> If folder_id is 0, I do the NULL comparison.
>
> SELECT *
> FROM mytable
> WHERE folder_id IS NOT DISTINCT FROM NULLIF(?, 0);

SELECT *
FROM mytable
WHERE folder IS NULL;

Or have your PHP say...

if ($fid == 0) {
$fid == "IS NULL"
}

(my php is extremely rusty but I think you get the idea)

And then build out your where clause.

>
> That seems to do what I want. Is it bad design? Something I'm missing
> about indexing a NULL or something like that?

Sincerely,

Joshua D. Drake

>
> -- Dante
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-01-05 05:59:33 Re: Need efficient way to do comparison with NULL as an option
Previous Message Scott Marlowe 2008-01-05 05:29:14 Re: large table vacuum issues