简体   繁体   English

连接单个表中的两列

[英]Concatenate two columns within a single table

I am new to mysql. 我是mysql新手。

This is my table structure 这是我的表结构

Test 测试

id |   A    |B    |
1  |   11   |     |
2  |   12   |     |
3  |   13   |     |
4  |   14   |abc  |

I want to concatenate column A and column B and get output like below 我想连接列A和B列,得到输出如下图所示

id |   C   |
1  |   11  |
2  |   12  |
3  |   13  |
4  |   abc |

My question is,is it possible to get such output in mysql? 我的问题是,是否有可能在mysql中获得这样的输出?

I think you are looking for the coalesce() function: 我认为您正在寻找coalesce()函数:

select id, coalesce(B, A) as C
from table t;

Use COALESCE() : 使用COALESCE()

SELECT id, COALESCE (B, A) AS C
FROM yourtable

coalesce() evalutes arguments left->right, and returns the first non-null value. coalesce()评估参数left-> right,并返回第一个非null值。

尝试这个:

SELECT ID, IFNULL(B,A) AS C FROM Test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM