From: | Brett Mc Bride <brett(dot)mcbride(at)deakin(dot)edu(dot)au> |
---|---|
To: | Andrus <kobruleht2(at)hot(dot)ee>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: How to force select to return exactly one row |
Date: | 2010-06-21 23:23:01 |
Message-ID: | 1451A17266557D4C9733B93ACC5DA5C1132D4F6B86@garnet-1.du.deakin.edu.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
How about:
SELECT * from (
SELECT somecolumns
FROM ko
LEFT JOIN t1 ON t1.primarykey= ko.t1foreignkwey
...
LEFT JOIN tn ON tn.primarykey= ko.tnforeignkwey
WHERE ko.primarykey='someprimarykeyvalue'
UNION ALL
SELECT default_value
)
LIMIT 1;
-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Andrus
Sent: Tuesday, 22 June 2010 5:08 AM
To: pgsql-general(at)postgresql(dot)org
Subject: [GENERAL] How to force select to return exactly one row
Autogenerated select statement contains 0 .. n left joins:
SELECT somecolumns
FROM ko
LEFT JOIN t1 ON t1.primarykey= ko.t1foreignkwey
...
LEFT JOIN tn ON tn.primarykey= ko.tnforeignkwey
WHERE ko.primarykey='someprimarykeyvalue';
This select can return only 0 or 1 rows depending if ko row with primary key
'someprimarykeyvalue' exists or not.
Problem:
if there is no searched primary key row in ko database, select should also
return empty row.
To get this result I added right join:
SELECT somecolumns
FROM ko
RIGHT JOIN (SELECT 1) _forceonerow ON true
LEFT JOIN t1 ON t1.primarykey= ko.t1foreignkwey
...
LEFT JOIN tn ON tn.primarykey= ko.tnforeignkwey
WHERE ko.primarykey is null or ko.primarykey='someprimarykeyvalue';
but it still does not return row if primary key row 'someprimarykeyvalue'
does not exist.
How to force this statement to return one row always ?
Andrus.
From | Date | Subject | |
---|---|---|---|
Next Message | Tim Landscheidt | 2010-06-21 23:41:20 | Re: How to force select to return exactly one row |
Previous Message | Brent Wood | 2010-06-21 22:38:04 | Re: How to force select to return exactly one row |