From: | Jonathan Moules <jonathan-lists(at)lightpear(dot)com> |
---|---|
To: | Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com> |
Cc: | <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: Always getting back a row, even with no results |
Date: | 2017-08-11 15:30:35 |
Message-ID: | 15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Do smth like along the lines (might want to add additional code for ordering, this just happens to run correctly) :
select * from my_table where cat = 500 UNION select null,null LIMIT CASE WHEN (select count(*) from my_table where cat=500)>0 THEN (select count(*) from my_table where cat=500) ELSE 1 END ;
Of course you can write a function to do that for you, but what made you want this in the first place? Maybe this is bad design ?
Unfortunately this is a constraint of the environment; not good design, but SQL seems like it should be the simplest place to solve this.
I tried your example; it seems the ordering is required, otherwise the results always include the null. So the below works for anyone who finds this in the future. Thanks
select
id
from
my_table
where
cat = 2
UNION
select
null as id
ORDER BY
id ASC
LIMIT
CASE
WHEN (
select
count(*)
from
my_table
where
cat = 2
) > 0
THEN (
select count(*) from my_table where cat=2
)
ELSE
1
END;
From | Date | Subject | |
---|---|---|---|
Next Message | Jonathan Moules | 2017-08-11 15:36:21 | Re: Always getting back a row, even with no results |
Previous Message | David G. Johnston | 2017-08-11 15:18:04 | Re: Always getting back a row, even with no results |