簡體   English   中英

在Yii CDbCriteria語句中使用CASE

[英]Using CASE in Yii CDbCriteria statement

誰能說出為什么這個CASE語句在我的Yii CDbCriteria語句中不起作用?

$criteria = new CDbCriteria;
$criteria->select = array('t.*',
        '(CASE
            WHEN t.type = 3 THEN "Quick Point"
            WHEN t.type = 2 THEN positive.reason
            WHEN t.type = 1 THEN deduction.reason
            END AS reason)');

我收到以下錯誤:

Active record "OrganisationClassroomRedeemedCodes" is trying to select an invalid column "(CASE 
WHEN t.type = 3 THEN "Quick Point"
WHEN t.type = 2 THEN positive.reason 
WHEN t.type = 1 THEN deduction.reason 
END AS reason)". Note, the column must exist in the table or be an expression with alias. 

我本質上是在嘗試使用CDbCriteria構建器進行此查詢

SELECT `t`.*,
CASE
 WHEN t.type = '3' THEN 'Quick Point' 
 WHEN t.type = '2' THEN positive.reason
 WHEN t.type = '1' THEN deduction.reason
  END AS reason
FROM
`organisation_classroom_redeemed_codes` `t`
 LEFT OUTER JOIN `organisation_classroom_achievements` `positive` ON (
`positive`.`achievement_id` = `t`.`order_product_id`
) 
LEFT OUTER JOIN `organisation_classroom_deductions` `deduction` ON (
`deduction`.`deduction_id` = `t`.`order_product_id` 
) 
WHERE (t.myuser_id = 12345)
ORDER BY `t`.`date_redeemed` DESC

如果對其他人有幫助...這是正確的方法

    $criteria->select = array('t.*',
        '(CASE
            WHEN t.type = 1 THEN deduction.reason
            WHEN t.type = 2 THEN positive.reason
            WHEN t.type = 3 THEN "custom"
            END) AS reason',           
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM