我正在尝试使用Yii' queryBuilder'构建复杂的SQL查询 - 现有查询如下所示:
$cmd->select('SUM(redeemedCodes.school_points) AS reportingTotalPoints, redeemedCodes.date_redeemed, redeemedCodes.inactive, redeemedCodes.subject_id);
$cmd->from('redeemed_codes redeemedCodes');
任何人都可以解释如何修改SELECT以包含IF元素,如下所示: IF(orderProduct.subject_message_link = 1 AND orderProduct.message!='',CONCAT(subject.subject_name,'',orderProduct.message),subject.subject_name)AS reportingSubjectName&#39 ;)
甚至可以使用Yii中的createCommand()吗?
我正在尝试构建类似下面的查询:
SELECT redeemedCodes.user_id AS user_id, SUM(redeemedCodes.school_points) AS reportingTotalPoints,
IF(orderProduct.subject_message_link = 1 AND orderProduct.message != '', CONCAT(subject.subject_name, ' ', orderProduct.message), subject.subject_name) AS reportingSubjectName
FROM `redeemed_codes` `redeemedCodes`
答案 0 :(得分:0)
未经测试:
我认为CDbExpression可能很方便。
$exp = new CDbExpression("IF(orderProduct.subject_message_link = 1 AND orderProduct.message != '', CONCAT(subject.subject_name, ' ', orderProduct.message), subject.subject_name)");
并在需要的地方使用$ exp来放置字符串。 e.g。
$sql = "Select *, " . $exp . " as ReportingSubjectName FROM ....";
答案 1 :(得分:-1)
$sql = "SELECT redeemedCodes.user_id AS user_id, SUM(redeemedCodes.school_points) AS reportingTotalPoints, redeemedCodes.date_redeemed, redeemedCodes.inactive, redeemedCodes.subject_id, IF(orderProduct.subject_message_link = 1 AND orderProduct.message != '', CONCAT(subject.subject_name, ' ', orderProduct.message), subject.subject_name) AS reportingSubjectName FROM `redeemed_codes` `redeemedCodes`";
$result = Yii::app()->db->createCommand($sql)->queryAll();