MySQL 将两列合并为一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22739841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:19:34  来源:igfitidea点击:

MySQL combine two columns into one column

mysqlsql

提问by aab

I'm trying to find a way to combine two columns into one, but keep getting the value '0' in the column instead to the combination of the words.

我试图找到一种将两列合并为一列的方法,但继续将列中的值 '0' 改为单词的组合。

These are what I've tried as well as others:

这些是我和其他人都尝试过的:

SELECT column1 + column2 AS column3
FROM table;

SELECT column1 || column2 AS column3
FROM table;

SELECT column1 + ' ' + column2 AS column3
FROM table;

Could someone please let me know what I'm doing wrong?

有人可以让我知道我做错了什么吗?

回答by Gordon Linoff

My guess is that you are using MySQL where the +operator does addition, along with silent conversion of the values to numbers. If a value does not start with a digit, then the converted value is 0.

我的猜测是您正在使用 MySQL,其中+运算符进行加法,以及将值静默转换为数字。如果值不以数字开头,则转换后的值为0

So try this:

所以试试这个:

select concat(column1, column2)

Two ways to add a space:

添加空格的两种方法:

select concat(column1, ' ', column2)
select concat_ws(' ', column1, column2)

回答by Oreniwa Babatunde

Try this, it works for me

试试这个,它对我有用

select (column1 || ' '|| column2) from table;

回答by sk juli kaka

It's work for me

这对我有用

SELECT CONCAT(column1, ' ' ,column2) AS newColumn;

回答by mattk

This is the only solution that would work for me, when I required a space in between the columns being merged.

当我需要在合并的列之间留出空间时,这是唯一对我有用的解决方案。

select concat(concat(column1,' '), column2)

回答by 2NinerRomeo

For the MySQL fans out there, I like the IFNULL()function. Other answers here suggest similar functionality with the ISNULL()function in some implementations. In my situation, I have a column of descriptions which is NOT NULL, and a column of serial numbers which may be NULLThis is how I combined them into one column:

对于那里的 MySQL 粉丝,我喜欢这个IFNULL()功能。此处的其他答案建议与ISNULL()某些实现中的函数具有类似的功能。在我的情况下,我有一列描述是NOT NULL,一列序列号可能是NULL这就是我将它们组合成一列的方式:

SELECT CONCAT(description,IFNULL(' SN: ', serial_number),'')) FROM my_table;

My results suggest that the results of concatenating a string with NULLresults in a NULL. I have been getting the alternative value in those cases.

我的结果表明,将字符串与NULL结果连接到NULL. 在这些情况下,我一直在获得替代价值。

回答by Balaji Dongare

If you are Working On OracleThen:

如果你正在工作,Oracle那么:

SELECT column1 || column2 AS column3
FROM table;

OR

或者

If You Are Working On MySql Then:

如果您正在使用 MySql,那么:

SELECT Concat(column1 ,column2) AS column3
FROM table;

回答by Sunil Acharya

I have used this way and Its a best forever. In this code null also handled

我用过这种方式,它永远是最好的。在此代码中 null 也处理

SELECT Title,
FirstName,
lastName, 
ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(LastName,'') as FullName 
FROM Customer

Try this...

尝试这个...

回答by Ritesh Yadav

convert(varchar, column_name1) + (varchar, column_name)

回答by Ravin

SELECT Collumn1 + ' - ' + Collumn2 AS 'FullName' FROM TableName