Re: Sequential select queries...?? - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Sequential select queries...??
Date
Msg-id web-105146@davinci.ethosmedia.com
Whole thread Raw
In response to Sequential select queries...??  (Mark Mikulec <mm98au@badger.ac.brocku.ca>)
List pgsql-sql
Mark,

> What i'd like to do is the following:
> 
> Select id from T where name = 'bleh';
> 
> and
> 
> Select id from T where description = 'bleh';
> 
> and result both results in the same result set. That is, duplicate
> id's
> if they appear. So then I could do a GROUP BY and a COUNT to see how
> many appeared in only one, and how many appeared in both.

What you want is UNION ALL.  You also want to go out and purchase "SQL
for Smarties" after you finish reading my e-mail.

SELECT id FROM T WHERE name = 'bleh'
UNION ALL
SELECT id FROM T WHERE description = 'bleh';

This gives you both result sets, once right after the other.  If you
didn't want to see duplicate values (i.e. only one instance of each
"id"), you would use simply UNION without the "ALL".

This means that it is possible to get both your desired rowcounts out of
a *single* query, using subselects.  "SQL for Smarties" can help you
learn to build this kind of query.

-Josh


______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


pgsql-sql by date:

Previous
From: "Robby Slaughter"
Date:
Subject: RE: Sequential select queries...??
Next
From: spam@freeuk.com (Stephen Patterson)
Date:
Subject: Finding table constraints