在Yii CDbCriteria语句中使用CASE

时间:2015-08-06 09:58:48

标签: php mysql yii

有人可以建议为什么这个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

1 个答案:

答案 0 :(得分:1)

如果它对其他人有帮助......这是正确的方法

.menu a{
  float: left;
  height: 50px;
}