MySQL 错误:SELECT list is not in GROUP BY clause - 解决方法
这个错误是因为在使用 GROUP BY 语句时,SELECT 语句中的某些列没有被包含在 GROUP BY 语句中,并且它们不属于聚合函数。这是 MySQL 的一个严格模式 (sql_mode=only_full_group_by) 下的限制。
解决方法是将所有 SELECT 语句中的列都包含在 GROUP BY 语句中,或者将它们都用聚合函数进行处理。在这个特定的查询中,可以将 'T.id' 列移除,或者将它用聚合函数进行处理,例如使用 MAX 或 MIN 函数:
SELECT MAX(T.id),b.id AS seaservice_id,a.create_time,b.talent_user_id,b.company_id,b.title,b.pay_amount,b.payment_way,b.currency,
b.completion_time,b.sea_service_status,b.service_address,
COALESCE(c.short_name_cn,e.realname, e.nickname, e.username) AS talentName,a.status AS applyStatus,IFNULL(d.has_withdraw,0) as hasWithdraw
FROM iw_seaservice_apply a
LEFT JOIN iw_seaservice b ON a.seaservice_id = b.id
LEFT JOIN iw_company c ON b.company_id = c.id
LEFT JOIN iw_seaservice_completion_apply d ON a.id = d.seaservice_apply_id
LEFT JOIN iw_talent_user e ON b.talent_user_id = e.id
${ew.customSqlSegment}
<if test="minPayAmount != null">
AND pay_amount >= #{minPayAmount}
</if>
<if test="maxPayAmount != null">
AND pay_amount <= #{maxPayAmount}
</if>
<if test="applyStatus != null">
AND applyStatus = #{applyStatus}
</if>
<if test="companyName != null and companyName != '' ">
AND companyName LIKE '${companyName}%'
</if>
GROUP BY seaservice_id
ORDER BY create_time DESC,seaservice_id DESC
这样修改后,查询语句将不再违反 MySQL 的严格模式,并将返回正确的结果。
原文地址: https://www.cveoy.top/t/topic/okyU 著作权归作者所有。请勿转载和采集!