gpt4 book ai didi

php - Yii Active Record COALESCE 不显示结果

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:00 26 4
gpt4 key购买 nike

我有以下查询:

$result = Table1::model()->findAll(array(
'with' => array(
'table2' => array(
'joinType' => 'LEFT JOIN',
'on' => 'pk = fk AND fk=1'
)
),
'select' => array('name',
'COALESCE(table2.price, t.standardprice) AS price',
'COALESCE(table2.period, t.period) AS period')
)
);

我的目标是选择表 2 的字段(如果已填写),但如果这些字段为空/未找到行,则应显示原始表的字段。

但是,我的输出并不像预期的那样。 price 字段根本没有显示在我的结果属性中,period 字段要么是 table2 的值,要么是空的。

编辑:也许我的 SQL 在某处出错了。使用这个 SQL 给我想要的结果:

SELECT name, COALESCE(tb1.price, tb2.standardprice) as price, COALESCE(tb1.period, tb2.period) as period
FROM table1 as tb1
LEFT JOIN table2 as tb2
ON (tb1.pk= tb2.fk) AND fk=1;

但我没有发现我当前的代码有任何不同。

EDIT2:表结构:

Table1(原始表)

pk            (int 11) - Primary key, auto increment
name (varchar 255)
standardprice (decimal 11,2)
period (varchar 255)
fkLanguage //not relevant
photo //not relevant
description //not relevant
link //not relevant

表2

ID      (int 11) - Primary key, auto increment
fk (int 11) - Foreign key, which links to pk of table1
period (varchar 255)
price (decimal 11,2)
fkType //not relevant
amount //not relevant

澄清:fk=1 确实是一个 JOIN 条件。如果 fk 不是 1,那么我不希望这些行加入,而是从 table1 中获取值。

最佳答案

您需要添加列 price 以解析架构中不存在的列。
尝试修改模型Table1()

  • 添加public $price;
  • 将方法 attributeNames 重写为以下内容:
public function attributeNames()
{
$colums = parent::attributeNames();
$colums[] = 'price';
return $colums;
}

关于php - Yii Active Record COALESCE 不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450758/

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