作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试按 ward_name 进行分组,这是我唯一需要分组的内容,但我收到此错误:
\ #1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'osunemonitor.r.entry_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
在我的查询中,如何使其仅按 ward_name
分组?
我的查询:
SELECT
pu.pu_ward_name,
entry_date,
pu.o_code,
pu.pu_ward_name,
pu.pu_lga_name,
pu.pu_code,
pu.pu_ward_code,
pu.pu_lga_code,
pu.pu_voters_reg_count,
SUM(total_vote_cast) AS total_vote_cast,
SUM(apc_total_vote) AS apc_total_vote,
SUM(pdp_total_vote) AS pdp_total_vote,
SUM(adp) AS adp, ward_name
FROM
post_election_info r
LEFT JOIN polling_unit pu ON r.ward_name = pu.pu_ward_name
WHERE lga_name = 'BORIPE'
GROUP BY
pu.pu_ward_name
最佳答案
每个非聚合列都必须出现在GROUP BY
子句中。这是 ANSI SQL 中的一个标准,尽管古代版本的 MySQL 曾经对此很宽松。
想一想:如果您SELECT
一列,但没有GROUP BY
它,RDBMS 如何可靠地选择应显示组中的哪个值?
可能的解决方案包括:
GROUP BY
子句pu_ward_name
之外的所有列使用聚合函数。例如,MAX() 和 MIN() 是字符串友好的(与 SUM() 不同)。第一个解决方案:
SELECT
pu.pu_ward_name,
entry_date,
pu.o_code,
pu.pu_lga_name,
pu.pu_code,
pu.pu_ward_code,
pu.pu_lga_code,
pu.pu_voters_reg_count,
SUM(total_vote_cast) AS total_vote_cast,
SUM(apc_total_vote) AS apc_total_vote,
SUM(pdp_total_vote) AS pdp_total_vote,
SUM(adp) AS adp
FROM
post_election_info r
LEFT JOIN polling_unit pu ON r.ward_name = pu.pu_ward_name
WHERE lga_name = 'BORIPE'
GROUP BY
pu.pu_ward_name,
entry_date,
pu.o_code,
pu.pu_lga_name,
pu.pu_code,
pu.pu_ward_code,
pu.pu_lga_code,
pu.pu_voters_reg_count
第二个解决方案:
SELECT
pu.pu_ward_name,
MAX(entry_date) AS entry_date,
MAX(pu.o_code) AS o_code,
MAX(pu.pu_lga_name) AS pu_lga_name,
MAX(pu.pu_code) AS pu_code,
MAX(pu.pu_ward_code) AS pu_ward_code,
MAX(pu.pu_lga_code) AS pu_lga_code,
MAX(pu.pu_voters_reg_count) AS pu_voters_reg_count,
SUM(total_vote_cast) AS total_vote_cast,
SUM(apc_total_vote) AS apc_total_vote,
SUM(pdp_total_vote) AS pdp_total_vote,
SUM(adp) AS adp
FROM
post_election_info r
LEFT JOIN polling_unit pu ON r.ward_name = pu.pu_ward_name
WHERE lga_name = 'BORIPE'
GROUP BY
pu.pu_ward_name
关于mysql - #1055 - SELECT 列表的表达式 #2 不在 GROUP BY 子句中并且包含非聚合列 'osunemonitor.r.entry_date',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54602444/
我是一名优秀的程序员,十分优秀!