2016-07-28 86 views
1

我創建了一個標準:Yii的CDbCriteria添加列

 $ids = array(1,2,3); 
     $criteria = new CDbCriteria; 
     $criteria->select = 'SUM(quantity) as quan'; 
     $criteria->with = array('order'); 
     $criteria->condition="order.customer = ".Yii::app()->user->id." AND order.status_id <> 4 AND order.status_id <> 3 AND order.type = 0"; 
     $criteria->addInCondition("product_id", $ids); 
     $order_prod = OrderProduct::model()->find($criteria); 

但是當我運行此我得到錯誤:

1140 In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'als.t.id'; this is incompatible with sql_mode=only_full_group_by. The SQL statement executed was: SELECT SUM(quantity) as quan, t . id AS t0_c0 , order . id AS t1_c0 , order . phone AS t1_c1 , order . email AS t1_c2 .

在標準選擇我只使用量的總和。

+0

它這個完整的查詢?顯示完整查詢 – user1234

回答

1

嗨禁用only_full_group_by

試試這個sql

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); 
+0

It works.thank's –

0

你想從OrderProduct表只的數量總和。因此,這將是最好不要使用活動記錄,但寫有查詢簡單的命令,像這樣:

$result = Yii::app()->db->createCommand(' 
    SELECT SUM(t.quantity) as quan 
    FROM order_product t 
    LEFT JOIN order ON order.ID = t.order_id 
    WHERE 
     order.customer = :userId AND 
     order.status_id NOT IN (3,4) AND 
     order.type = 0 AND 
     t.product_id IN (1,2,3) 
')->queryScalar([ 
    ':userId' => Yii::app()->user->id, 
]); 

這裏的「分貝」是你CDbConnection組件的名稱。 OrderProduct表名和聯接條件在您的系統中可能會有所不同。

+0

好主意......謝謝 –