Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 从子查询结果中 进行 GROUP_CONCAT … ORDER BY 操作,会报列不存在的错误
[TiDB Usage Environment] Production Environment
[TiDB Version] TiDB6.1
[Reproduction Path]
sql1: This SQL execution will report an error:
> 1105 - Can't find column Column#57 in schema Column: [pre_dim.dim_performance_roster_month.role_id,pre_dim.dim_performance_roster_month.role_name,pre_dim.dim_performance_roster_month.role_id,pre_dim.dim_performance_roster_month.role_name] Unique key: []
SELECT
GROUP_CONCAT(DISTINCT `role_id` ORDER BY `role_id` ASC SEPARATOR '&&' ) role_ids,
GROUP_CONCAT(DISTINCT `role_name` ORDER BY `role_id` ASC SEPARATOR '&&' ) role_names
FROM(
SELECT
COALESCE(b.role_id,a.role_id) role_id
,COALESCE(b.role_name,a.role_name) role_name
FROM pre_dim.`dim_performance_roster_month` a
LEFT JOIN pre_dim.`dim_performance_roster_month` b ON a.user_id = b.user_id
) a1
sql2: This SQL execution (removed order by …) has no issues, no errors
SELECT
GROUP_CONCAT(DISTINCT `role_id` ) role_ids,
GROUP_CONCAT(DISTINCT `role_name` ) role_names
FROM(
SELECT
COALESCE(b.role_id,a.role_id) role_id
,COALESCE(b.role_name,a.role_name) role_name
FROM pre_dim.`dim_performance_roster_month` a
LEFT JOIN pre_dim.`dim_performance_roster_month` b ON a.user_id = b.user_id
) a1
sql3: Putting the subquery into a table (test.test_dim_performance_roster_month) and then querying, no issues
SELECT
GROUP_CONCAT(DISTINCT `role_id` ORDER BY `role_id` ASC SEPARATOR '&&' ) role_ids,
GROUP_CONCAT(DISTINCT `role_name` ORDER BY `role_id` ASC SEPARATOR '&&' ) role_names
FROM(
SELECT * from test.test_dim_performance_roster_month
) a1
[Encountered Problem: Problem Phenomenon and Impact]
[Resource Configuration]
[Attachments: Screenshots/Logs/Monitoring]