Add row to query result using select

jdsushi picture jdsushi · Apr 10, 2009 · Viewed 135.7k times · Source

Is it possible to extend query results with literals like this?

select name from users
union
select name from ('JASON');

or

select age, name from users
union
select age, name from (25,'Betty');

so it returns all the names in the table plus 'JASON', or (25,'Betty').

Answer

Quassnoi picture Quassnoi · Apr 10, 2009

You use it like this:

SELECT  age, name
FROM    users
UNION
SELECT  25 AS age, 'Betty' AS name

Use UNION ALL to allow duplicates: if there is a 25-years old Betty among your users, the second query will not select her again with mere UNION.