Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用sql合并两列?_Mysql_Sql - Fatal编程技术网

Mysql 如何使用sql合并两列?

Mysql 如何使用sql合并两列?,mysql,sql,Mysql,Sql,鉴于此表: id foo bar 1 a d 2 b e 3 c f 我想合并foo和bar列,以便得到它们所有值的结果集: 预期结果: a b c d e f 我的搜索结果表明,其答案是通过以下方式对字段进行加密: select CONCAT(foo, bar) as foobar from MyTable 为我生成错误的输出: ad be cf 我不想合并,但我想合并这两列 如何获得两个字段的foo栏的所有值的结果?使

鉴于此表:

id    foo    bar
 1    a      d
 2    b      e
 3    c      f
我想合并foo和bar列,以便得到它们所有值的结果集:

预期结果:

a
b
c
d
e
f
我的搜索结果表明,其答案是通过以下方式对字段进行加密:

select CONCAT(foo, bar) as foobar from MyTable
为我生成错误的输出:

ad
be
cf
我不想合并,但我想合并这两列

如何获得两个字段的
foo
栏的所有值的结果?

使用Union all

      Select foo from a
        Union all
      Select bar from a
使用Union all

      Select foo from a
        Union all
      Select bar from a
使用UNION ALL:

select foo from tablename
union all
select bar from tablename
使用UNION ALL:

select foo from tablename
union all
select bar from tablename
使用
UNION

SELECT foo as new_col FROM MyTable
UNION
SELECT bar FROM MyTable
或者如果您对重复项感兴趣,请使用
UNION-ALL

使用
UNION

SELECT foo as new_col FROM MyTable
UNION
SELECT bar FROM MyTable
SELECT foo FROM t
UNION ALL
SELECT bar FROM t
或者,如果您对重复项感兴趣,请将所有项合并。

那么

SELECT foo FROM t
UNION ALL
SELECT bar FROM t
select foo from TABLE UNION ALL select bar from TABLE
那么

select foo from TABLE UNION ALL select bar from TABLE

与仅使用
UNION
相比有什么区别?UNION all也将提供重复值,UNION将丢弃重复值,但是在您的示例数据中没有重复值,因此union和union都提供相同的结果与仅使用
union
?union all也将提供重复值,union丢弃重复值,但是在您的示例数据中没有重复的值,所以union和union都会给出相同的结果为什么是DOWNDOVE?为什么是DOWNDOVE?