How To Form this MySQL Query?
I want to create a MySQL Query similar to
SELECT city, state, country FROM location
UNION
SELECT 'Chicago','Illinois', 'USA' IF 'Chicago' NOT IN (SELECT city FROM location);
Basically, if 'Chicago' is not returned in the results, I want to append it to the results. Problem is, the query expects a FROM
table. Do I u开发者_开发技巧se a dummy table, or am I doing this all wrong?
Just write the second SELECT
with no conditions and let the UNION
do the work for you. UNION
will remove duplicates from the result set, so there's no harm in repeating "Chicago" in the second query. If "Chicago" also occurs in your first SELECT
, the UNION
will remove the duplicate created by your second query.
SELECT city, state, country FROM location
UNION
SELECT 'Chicago','Illinois', 'USA'
精彩评论