[英]MySQL combine two columns into one column
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?有人可以让我知道我做错了什么吗?
My guess is that you are using MySQL where the +
operator does addition, along with silent conversion of the values to numbers.我的猜测是您正在使用 MySQL,其中
+
运算符进行加法,以及将值静默转换为数字。 If a value does not start with a digit, then the converted value is 0
.如果值不以数字开头,则转换后的值为
0
。
So try this:所以试试这个:
select concat(column1, column2)
Two ways to add a space:添加空格的两种方法:
select concat(column1, ' ', column2)
select concat_ws(' ', column1, column2)
试试这个,它对我有用
select (column1 || ' '|| column2) from table;
这对我有用
SELECT CONCAT(column1, ' ' ,column2) AS newColumn;
当我需要在合并的列之间留出空间时,这是唯一对我有用的解决方案。
select concat(concat(column1,' '), column2)
If you are Working On Oracle
Then:如果您在
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;
For the MySQL fans out there, I like the IFNULL()
function.对于 MySQL 爱好者,我喜欢
IFNULL()
函数。 Other answers here suggest similar functionality with the ISNULL()
function in some implementations.此处的其他答案建议在某些实现中与
ISNULL()
函数具有类似的功能。 In my situation, I have a column of descriptions which is NOT NULL
, and a column of serial numbers which may be NULL
This is how I combined them into one column:在我的情况下,我有一列
NOT NULL
的描述和一列可能为NULL
的序列号这就是我将它们组合成一列的方式:
SELECT CONCAT(description,IFNULL(' SN: ', serial_number),'')) FROM my_table;
My results suggest that the results of concatenating a string with NULL
results in a NULL
.我的结果表明,将字符串与
NULL
连接的结果会产生NULL
。 I have been getting the alternative value in those cases.在这些情况下,我一直在获得替代价值。
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...尝试这个...
table:桌子:
---------------------
| column1 | column2 |
---------------------
| abc | xyz |
---------------------
In Oracle
:在
Oracle
:
SELECT column1 || column2 AS column3
FROM table_name;
Output:输出:
table:桌子:
---------------------
| column3 |
---------------------
| abcxyz |
---------------------
If you want to put ','
or '.'
如果你想放
','
或'.'
or any string within two column data then you may use:或两列数据中的任何字符串,然后您可以使用:
SELECT column1 || '.' || column2 AS column3
FROM table_name;
Output:输出:
table:桌子:
---------------------
| column3 |
---------------------
| abc.xyz |
---------------------
convert(varchar, column_name1) + (varchar, column_name)
SELECT Column1 + ' - ' + Column2 AS 'FullName' FROM TableName
SELECT CONVERT (nvarchar (10), Month(NextDate))+'-'+CONVERT (nvarchar (10), Year(NextDate)) as MonthOfYear, COUNT(CaseNo) as CountDisposal
FROM dbo.Main_Cause_List
WHERE (DisposalState = 'Dispossed-off')
GROUP BY Month(NextDate),Year(NextDate);
try to use coalesce()
and concat()
to combine columns in the SQL query.尝试使用
coalesce()
和concat()
组合SQL 查询中的列。
Assume that you have 4 columns (id, name, phone_number, country_code) in a user table and you want to print phone number in this format: +countrycodephonenumber假设您在用户表中有 4 列(id、name、phone_number、country_code)并且您想以这种格式打印电话号码: +countrycodephonenumber
Eg: 1, vishnu, 9961907453, 91 will return phone number as +919961907453.例如:1, vishnu, 9961907453, 91 将返回电话号码为 +919961907453。
You can use the following query to get the above result.您可以使用以下查询来获得上述结果。
select coalesce(concat('+', country_code, phone_number)) from user;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.