gpt4 book ai didi

mysql - Cakephp 虚拟字段如果此列为空则使用此列

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:57 25 4
gpt4 key购买 nike

我有一个名为“Contacts”的表,我想创建一个名为 customer 的虚拟字段。

联系人有列,名为 first_name、last_name、company。

我通过连接所有三个来创建虚拟字段

public $virtualFields = array(
'customer' => "CONCAT(Contact.first_name, ' ', Contact.last_name, ' ', Contact.company)"
);

但是我怎样才能创建它以便它只显示公司,但如果它为 null 或空字符串,它将显示名字和姓氏

最佳答案

您可以尝试将 SQL IF 语句放入其中:

public $virtualFields = array(
'customer' => "IF(Contact.company IS NULL OR Contact.company = '', CONCAT(Contact.first_name, ' ', Contact.last_name, ' ', Contact.company), Contact.company)"
);

is null or is empty 而言,您的数据库是否没有一致的空值?如果没有,它应该而且您应该解决它...

关于mysql - Cakephp 虚拟字段如果此列为空则使用此列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20383445/

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