mysql - 如何在mysql中启用only_full_group_by的情况下获取group by语句的单个值

标签 mysql group-by

我正在尝试让任何一位客户与这样的公司建立联系:

select company_id, id from `customer` 
where company_id IS NOT NULL 
group by `company_id`

我收到错误:

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column customer.id which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

如何在不禁用 only_full_group_by 的情况下实现此目的?

最佳答案

GROUP BY 旨在与聚合函数一起使用。使用 LIMIT 获取一行。

select company_id, id from `customer` 
where company_id IS NOT NULL 
limit 1

编辑:(问题澄清后):

如果您想要 company_id 和 customer_id 的每个唯一组合,请使用 DISTINCT:

select distinct company_id, id 
from `customer` 
where company_id IS NOT NULL 

关于mysql - 如何在mysql中启用only_full_group_by的情况下获取group by语句的单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50400338/

相关文章:

mysql - 设置下层数据库mysql

mysql - 如何在连接表后使用where子句?

c# - 在服务器问题上使用反斜杠保存路径名称

php - Mysql:在组合列中搜索

mysql - 根据更新时的特定条件更新另一个表的最佳方法是什么?

mysql - 计算每个项目和日期范围内的每个月库存中的项目数量 - mysql

ruby-on-rails - 不在 Heroku 上工作的 RoR Postgresql 时区组

mysql - 获取组的总和以及最后一个值

mysql - 无法使用 group by 查询获取 MySQL 中的某些字段

python - 如何在神社中循环每个唯一变量?