Question : Is it possible to have a WHERE clause in a Union Select Query?

Is it possible to have a WHERE clause in a Union Select Query?  I have this simple test query that delivers all the records in the table regardless of the value of the CodeType field.

SELECT tblCodes.CodeType FROM tblCodes
UNION SELECT "CeremonyStatus" as CodeType FROM tblCodes
WHERE (((tblCodes.CodeType)="CeremonyStatus"));

Thanks in advance.

Answer : Is it possible to have a WHERE clause in a Union Select Query?

SELECT CodeType From
(
SELECT tblCodes.CodeType FROM tblCodes
UNION
SELECT "CeremonyStatus" as CodeType FROM tblCodes
) AS TMP
WHERE (((TMP.CodeType)="CeremonyStatus"));

This will
produce all the rows from the table (count=x),
then all the rows again (every row has the codetype hard-set to "CeremonyStatus"), (count=x)
then UNION them together followed by DISTINCT (count=2x, distinct down to count=x)
This becomes the subquery TMP.
The WHERE clause is applied to TMP, which results in one single result, "CeremonyStatus"

In short, yes you can WHERE a UNION, just subquery it.
Random Solutions  
 
programming4us programming4us