开关的情况下
问题描述:
我们能有一个开关罩查询更Yii2 ActiveQuery方式比低于核心SQL?开关的情况下
$comboGraph = \Yii::$app->db->createCommand('select class , count(*) registered, '.
'sum(case when payment_status = \'1\' then 1 else 0 end) paid, '.
'sum(case when payment_status = \'0\' then 1 else 0 end) unpaid '.
'from students group by class ORDER BY FIELD(class,\'V\',\'VI\',\'VII\')')
->queryAll();
我得到的结果,即使我用上面的查询,但我想我的东西更Yii2具体。那么有什么建议?
答
使用createCommand的是Yii2投诉。(和目前有没有何时..报表,这些statementes由扁平SQL指令管理管理时的具体功能)
但可能是你想使用查询生成器和相关功能.. http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html
$comboGraph = (new \yii\db\Query())
->select("class
, count(*) registered
, sum(case when payment_status = '1' then 1 else 0 end) paid
, sum(case when payment_status = '0' then 1 else 0 end) unpaid")
->from('students')
->groupBy('class')
->orderBy("FIELD(class,'V','VI','VII')")
->all();
您可以使用单引号和双引号的正确组合为更好的阅读