GROUP BY 查询总报错

来源:5-7 [PDO] 条件查询方法的调试与验证

wengyaqiang

2018-01-25 16:02

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yatest.user2.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

写回答 关注

3回答

  • 江南1号
    2018-03-17 21:11:28
    已采纳

    检查一下你的 select 的字段 ,取消里面不能分组的字段。

    wengya...

    非常感谢!

    2018-03-20 10:27:30

    共 1 条回复 >

  • 慕勒1208424
    2018-03-26 18:26:31

    謝謝 江南一號的回覆與解釋

    我不明白的是,套以下的函示寫出你說的 " select status, count(*) from user where id<=10 group by status " 該怎麼寫?
    是 print_r($PdoMySQL->find($tables,'id>=5','count(*)','status'));會出現錯誤 
    還是需要修改ParsFields函式?如何改?抱歉阿,我真的還是基礎班的....


    public static function find($tables,$where=null,$fields='*',$group=null,$having=null,$order=null,$limit=null){
            $sql='SELECT ' .self::parseFields($fields).' FROM '.$tables
            .self::parseWhere($where)
            .self::parseGroup($group)
            .self::parseHaving($having)
            .self::parseOrder($order)
            .self::parseLimit($limit);
            $dataAll=self::getAll($sql);
            return count($dataAll)==1?$dataAll[0]:$dataAll; // 三元運算子
        }
    public static function parseFields($fields){
            // 如是數組/陣列
            if(is_array($fields)){
                array_walk($fields,array('PdoMySQL','addSpecialChar')); // array_walk() 回調函式
                $fieldsStr=implode(',',$fields);
            // 如是字串
            }elseif(is_string($fields)&&!empty($fields)){
                if(strpos($fields,'`')===false){
                    $fields=explode(',',$fields);
                    array_walk($fields,array('PdoMySQL','addSpecialChar'));
                    $fieldsStr=implode(',',$fields);
                }else{
                    $fieldsStr=$fields;
                }
            }else{
                $fieldsStr='*';
            }
            return $fieldsStr;
        }


  • 慕勒1208424
    2018-03-21 21:55:01

    出現一樣的問題,當執行這句  print_r($PdoMySQL->find($tables,'id<=10','*','status'));
    出現這樣的錯誤訊息:

    SQLSTATE : 42000 
    SQL Error : Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'imooc.user.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
    Error SQL : SELECT * FROM user WHERE id<=10 GROUP BY status

    江南1号所說的“检查一下你的 select 的字段 ,取消里面不能分组的字段”不太懂該怎麼做....求解,感謝先~


    江南1号

    针对 status 字段分组,选择列可以把它加上。 即:select status, count(*) from user where id<=10 group by status

    2018-03-23 22:47:24

    共 2 条回复 >

PDO—数据库抽象层

本教程主要通过实战演练深入剖析PDO以加深理解

30043 学习 · 396 问题

查看课程

相似问题