我試圖重現一個ActiveRecord標準的SQL語句:SQL ActiveRecord不同標準
SELECT COALESCE(price, standardprice) AS price
FROM table1
LEFT JOIN table2
ON (pkTable1= fkTable1)
WHERE pkTable1= 1
到目前爲止,我有以下幾點:
$price = Table1::model()->find(array(
"select" => "COALESCE(price, standardprice) AS price",
'with' => array(
'table2' => array(
'joinType' => 'LEFT JOIN',
'on' => 'pkTable1= fkTable1',
)
),
'condition' => 'pkTable1=:item_id',
'params' => array(':item_id' => 1)
));
但這會導致到以下錯誤:'Active record "Table1" is trying to select an invalid column "COALESCE(price". Note, the column must exist in the table or be an expression with alias.
雖然列應該存在,但下面是兩個表結構:
表1
pkTable1 int(11) - Primary key
standardprice decimal(11,2)
name varchar(255) //not important here
category varchar(255) //not important here
表2
pkTable2 int(11) - Primary key //not important here
fkType int(11) - Foreign key //not important here
fkTable1 int(11) - Foreign key, linking to Table1
price decimal(11,2)
我究竟在做什麼錯?
嘗試替換您的選擇,如下所示:'「select」=> array(「COALESCE(price,standardprice)AS price」)'。這可能有助於理解逗號不是列分隔符。 –