简体   繁体   English

MySQL 5.7无法与组一起正常使用

[英]mySQL 5.7 not working correctly with groups

I have upgraded my datababase to mysql 5.7.15, and I keep getting this error message everytime I try to run the following query 我已经将我的数据库升级到mysql 5.7.15,每次尝试运行以下查询时,我都会不断收到此错误消息

SELECT `hired_proj_id` 
FROM `hired_projects` 
WHERE `status_code` = '3' 
  AND `client_id` = 55 
GROUP BY `proj_id` 
ORDER BY `hired_proj_id` DESC;

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sandbox.hired_projects.hired_proj_id' which is not functionally dependent on columns in GROUP BY clause; SELECT列表的表达式#1不在GROUP BY子句中,并且包含未聚合的列'sandbox.hired_projects.hired_proj_id',该列在功能上不依赖于GROUP BY子句中的列; this is incompatible with sql_mode=only_full_group_by 这与sql_mode = only_full_group_by不兼容

I have been look through stackover flow, but was unable to find an answer. 我一直在研究stackover流程,但是找不到答案。 Any help would be really appreciated. 任何帮助将非常感激。 Thanks!! 谢谢!!

I learned yesterday this thanks to Gordon Linoff 昨天我对戈登·利诺夫(Gordon Linoff)表示感谢

All columns in the select should either be columns in the group by or use aggregate functions (sum(), avg(), and so on). select中的所有列应为group by中的列,或使用聚合函数(sum(),avg()等)。

Your query: 您的查询:

  SELECT `hired_proj_id` 
    FROM `hired_projects` 
   WHERE `status_code` = '3' 
     AND `client_id` = 55 
GROUP BY `proj_id` 
ORDER BY `hired_proj_id` DESC

When we check your query we see that the selected 'hired_proj_id' is not in your GROUP BY . 当我们检查您的查询时,我们看到所选的'hired_proj_id'不在您的GROUP BY

Your query should be like this: 您的查询应如下所示:

    SELECT `hired_proj_id` 
      FROM `hired_projects` 
     WHERE `status_code` = '3' 
       AND `client_id` = 55 
  GROUP BY `hired_proj_id`,`proj_id` 
  ORDER BY `hired_proj_id` DESC

This is probably your solution. 这可能是您的解决方案。

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column SELECT列表的表达式#1不在GROUP BY子句中,并且包含未聚合的列

the error is clear, either you add the field on the SELECT to the GROUP BY 错误很明显,您可以将SELECT字段添加到GROUP BY

   SELECT <fieldName>
   FROM yourTable
   WHERE <conditions>
   GROUP BY <fieldName>

OR add an aggregated function on the SELECT 或在SELECT上添加聚合函数

   SELECT SUM(<fieldName>)
   FROM yourTable
   WHERE <conditions>
   GROUP BY <otherfieldName>

Note: MySQL is insecure and is deprecated, use PDO or MySQLi instead. 注意:MySQL不安全且已弃用,请改用PDO或MySQLi。

Here is an implementation with MySQLi, 这是MySQLi的实现,

 <?php

     //MySQLi information

        $db_host     = "localhost";
        $db_username = "username";
        $db_password = "password";

        //connect to mysqli database (Host/Username/Password)
        $connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());

        //select MySQLi dabatase table
        $db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());

  $sql = mysqli_query($connection, "

    SELECT `hired_proj_id` 
      FROM `hired_projects` 
     WHERE `status_code` = '3' 
       AND `client_id` = 55 
  GROUP BY `hired_proj_id`,`proj_id` 
  ORDER BY `hired_proj_id` DESC

");

    ?>

I am guessing that the query you really want is: 我猜您真正想要的查询是:

SELECT DISTINCT hired_proj_id 
FROM hired_projects
WHERE status_code = 3 AND client_id = 55 
ORDER BY hired_proj_id DESC;

Notes: 笔记:

  • Don't enclose numeric constants in single quotes -- unless they are really strings. 不要将数字常量用单引号引起来-除非它们确实是字符串。
  • No need to use backticks everywhere. 无需到处使用反引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM