在"选择查询"中包含一个字符串

Hen*_*son 2 mysql sql

我想知道我们可以在下面进行这个查询吗?

SELECT America, England, DISTINCT (country) FROM tb_country
Run Code Online (Sandbox Code Playgroud)

这将(我的意图是)显示:

America  
England  
(List of distinct country field in tb_country)
Run Code Online (Sandbox Code Playgroud)

所以重点是显示(例如)美国和英格兰,即使DISTINCT国家字段没有返回任何内容.基本上我需要这个查询列出一个选择下拉列表,并给出一些用户可以选择的粘性值,同时允许他们自己添加一个新的国家.

不言而喻,如果tb_country中的一行具有America或England的值,它们将不会在查询结果中显示为重复.因此,如果tb_country具有值列表:

Germany  
England  
Holland
Run Code Online (Sandbox Code Playgroud)

查询只会输出:

America  
England  
Germany  
Holland
Run Code Online (Sandbox Code Playgroud)

OMG*_*ies 7

您需要使用UNION:

SELECT 'America' AS country
UNION
SELECT 'England' AS country
UNION 
SELECT DISTINCT(c.country) AS country
  FROM TB_COUNTRY c
Run Code Online (Sandbox Code Playgroud)

UNION将删除重复项; UNION ALL不会(但速度更快).

数据类型必须与SELECT子句中的每个序号位置匹配.意思是,如果第一个查询中的第一列是INT,那么之后所有联合语句的第一列也需要是INT或NULL.