mySQL 5.7无法正常使用组

时间:2017-08-21 22:32:36

标签: php mysql

我已将我的数据库升级到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;

SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列'sandbox.hired_projects.hired_proj_id',它在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by

不兼容

我一直在浏览stackover流程,但无法找到答案。任何帮助将非常感激。谢谢!

4 个答案:

答案 0 :(得分:1)

昨天我得知戈登·林诺夫(Gordon Linoff)

  

select中的所有列都应该是or中的列   使用聚合函数(sum(),avg()等)。

您的查询:

  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

当我们检查您的查询时,我们发现所选的'hired_proj_id'不在您的GROUP BY中。

您的查询应该是这样的:

    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

这可能是你的解决方案。

答案 1 :(得分:0)

  

SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列

错误很明显,您可以将SELECT上的字段添加到GROUP BY

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

或者在SELECT

上添加汇总功能
   SELECT SUM(<fieldName>)
   FROM yourTable
   WHERE <conditions>
   GROUP BY <otherfieldName>

答案 2 :(得分:0)

注意:MySQL不安全且已弃用,请改用PDO或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

");

    ?>

答案 3 :(得分:0)

我猜你真正想要的问题是:

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

注意:

  • 不要将数字常量括在单引号中 - 除非它们实际上是字符串。
  • 无需在任何地方使用反叛。