开发者

How To Form this MySQL Query?

开发者 https://www.devze.com 2023-03-21 16:04 出处:网络
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);

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' 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号