Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
如何使用汇总编写find():

 $leadsCount = Approval::find()
                ->select(['COUNT(id) AS cnt, coalesce(status, "total")'])
                ->groupBy(['status'])
                ->with(rollup)
                ->all();


在运行查询时
得到这样的错误:使用未定义的常量汇总-假定为“汇总”

最佳答案

我想你的意思是:

 $leadsCount = Approval::find()
                ->select(['COUNT(id) AS cnt, coalesce(`status`, "total")'])
                ->groupBy(new \yii\db\Expression('`status` ASC WITH ROLLUP'))
                ->all();


方法with()是关于对象关系的,完全不同。

您还需要引用列状态,因为它在MySQL中是reserved word

顺便说一句:您可能还想使用asArray()->asArray()->all();),因为在此查询中没有获得Approval对象。

关于php - 如何在yii2中为汇总编写查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47323632/

10-13 05:18