gpt4 book ai didi

子字符串不循环的 MySQL GROUP BY

转载 作者:行者123 更新时间:2023-11-30 23:29:08 26 4
gpt4 key购买 nike

我正在尝试按子字符串分组。我只得到一个结果:

Region = Coastal, Value (R) = 1144900.

我应该得到 2 个结果。

这是我的代码:

SELECT
DISTINCT SUBSTRING_INDEX(`team_name`, '-', 1) AS 'Region',
SUM(`opportunities`.`value`) AS 'Value (R)'
FROM
`opportunities`
LEFT JOIN cf_type_link_data ON cf_type_link_data.sourceid = opportunities.id
LEFT JOIN cf_lu_type_fields ON cf_type_link_data.fieldid = cf_lu_type_fields.id
LEFT JOIN cf_lu_types_fields_dropdown ON cf_type_link_data.`value` = cf_lu_types_fields_dropdown.id AND cf_lu_type_fields.id = cf_lu_types_fields_dropdown.fieldid
LEFT JOIN `lu_teams` ON `lu_teams`.`contactid` = `opportunities`.`user_allocation`
LEFT JOIN `teams` ON `teams`.`id` = `lu_teams`.`teamid`
LEFT JOIN `lu_opportunity_status` ON `lu_opportunity_status`.`id` = `opportunities`.`status`
WHERE 1
AND `cf_lu_types_fields_dropdown`.`values` = 'Building Project'
AND cf_lu_type_fields.fieldname = 'Scaffolding Segment'
AND (`opportunities`.`expecteddate` >= '2012-01-01' AND `opportunities`.`expecteddate` <= '2012-07-24')
GROUP BY 'Region'
ORDER BY cf_lu_types_fields_dropdown.`values`;

感谢任何帮助。

最佳答案

您不能在 GROUP BY 子句中使用与 select 相同的别名

尝试

SELECT DISTINCT SUBSTRING_INDEX(team_name, '-', 1) AS 'Region', 
SUM(opportunities.value) AS 'Value (R)'....

GROUP BY SUBSTRING_INDEX(team_name, '-', 1)
ORDER BY cf_lu_types_fields_dropdown.values;

关于子字符串不循环的 MySQL GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11631182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com