来自其他两列的非空值的MySQL列
我需要知道在MySQL中是否可以这样做。有这样一张桌子:来自其他两列的非空值的MySQL列,mysql,Mysql,我需要知道在MySQL中是否可以这样做。有这样一张桌子: | ID | Column1 | Column2| +-----------------------+ | 1 | 1 | null | | 2 | null | 2 | | 3 | 3 | null | 获取组合column1和column2的列: | ID | Column3 | +--------------+ | 1 | 1 | | 2 | 2 |
| ID | Column1 | Column2|
+-----------------------+
| 1 | 1 | null |
| 2 | null | 2 |
| 3 | 3 | null |
获取组合column1和column2的列:
| ID | Column3 |
+--------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
因此,逻辑应该是针对每一行,如果column1的值不同于null,那么column3将是该值,如果不是,那么将是column2中的值
谢谢。如果为空,请使用
SELECT ID, IFNULL(Column1,Column2) AS Column3 FROM TABLE;
如果column1为空,则使用column2
SELECT ID, IFNULL(Column1,Column2) AS Column3 FROM TABLE;
如果column1为空,则使用column2使用
合并
获取列的第一个非空值
select id, coalesce(column1, column2) as column3
from your_table
使用
coalesce
获取列的第一个非空值
select id, coalesce(column1, column2) as column3
from your_table
您可以使用这个用例
select id, case when column1 is null then column2
when column2 is null the comumn1 end as column3
从你的桌子上
如果为空
select id, ifnull( column1 , column2 ) as column3
from your_table
或结合
select id, coalesce( column1 , column2 ) as column3
from your_table
您可以使用这个用例
select id, case when column1 is null then column2
when column2 is null the comumn1 end as column3
从你的桌子上
如果为空
select id, ifnull( column1 , column2 ) as column3
from your_table
或结合
select id, coalesce( column1 , column2 ) as column3
from your_table
您可以使用coalesce()或ifnull()函数实现所需的输出:
select id, coalesce(column1, column2) as column3 from yourtable
注意:如果两列都为null,则上述表达式将返回null。如果两列都不为null,则使用column1中的值。您可以使用coalesce()或ifnull()函数实现所需的输出:
select id, coalesce(column1, column2) as column3 from yourtable
注意:如果两列都为null,则上述表达式将返回null。如果两列都不为空,则使用column1中的值。查找大小写。。。当我考虑这个案子的时候。。。什么时候