php - 在没有 GROUP BY 的聚合查询中,SELECT 列表的表达式 #2 包含非聚合列 'a.b.id' ;

标签 php mysql mysqli

请帮我解决这个问题。 我的代码:

$dn1 = mysqli_fetch_array(mysqli_query($con, '
select count(id) as recip
     , id as recipid
     , (select count(*) from pm) as npm 
  from users 
 where username="'.$recip.'"'
 ));

它给出以下错误

Error: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'omapm.users.id'; this is incompatible with sql_mode=only_full_group_by

请帮我改正。 谢谢!

最佳答案

$dn1 = mysqli_fetch_array(mysqli_query($con, 
    'select count(id) as recip, id as recipid, 
        (select count(*) from pm) as npm 
    from users 
    where username="'.$recip.'"
    group by recipid, npm'));

无论如何你不应该在 sql 命令中使用 php var .. 这样你就有 sqlinjection 的风险.. 你应该检查你的 mysql 驱动程序以准备语句和绑定(bind)参数..

关于php - 在没有 GROUP BY 的聚合查询中,SELECT 列表的表达式 #2 包含非聚合列 'a.b.id' ;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023926/

相关文章:

PHP/mySQL 下拉框问题

mysql - MYSQL中针对子查询的查询优化?

php - 转到另一个用户页面

php - 在 MySQLi 中使用 5 INNER JOIN 可以吗?

php - 通过 htaccess 和分页限制获取值(value)

php - 是否有 PHP 版本的 zxcvbn.js 或者我可以在服务器端使用它来强制执行密码规则的方法?

php - 如何编写具有多个条件的 Doctrine 查询?

php - Google Analytics api PHP 使用未加密的用户名和密码

mysql - 用于逗号分隔值的 REGEX mysql

php - 与 mysqli() 的连接不起作用,为什么?