我正在尝试让任何一位客户与这样的公司建立联系:
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/