gpt4 book ai didi

mysql - 使用 MySql 中的 if 语句打印带有条件约束的新列

转载 作者:行者123 更新时间:2023-11-29 10:16:03 24 4
gpt4 key购买 nike

我有一个表“actor”,其字段为“pk”、“fname”、lname。我想打印一列(让我们称之为这个名称),默认情况下打印 fname,但如果 fname 为 null,则打印 lname。

我使用的查询是:

select if(fname = null,lname,fname) as name from actor;

但这并没有实现给定的任务。即使值为 null,它也会打印 fname。

最佳答案

NULL 进行比较您需要使用的值 IS NULL :

select if(fname IS NULL,lname,fname) as name from actor;

<=> :

select if(fname <=> null,lname,fname) as name from actor;
<小时/>

在您的示例中,最简单的方法是使用 COALESCE :

SELECT COALESCE(fname, lname) AS name FROM actor;

关于mysql - 使用 MySql 中的 if 语句打印带有条件约束的新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50103548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com