MySQL 别名在 CASE 语句的 ELSE 部分失败(错误代码:1054。'field list' 中的未知列 'xxx')

MySQL alias fails in ELSE part of CASE statement (Error Code: 1054. Unknown column 'xxx' in 'field list')

我在网上搜索了一整天,找不到这个问题的答案。

MySQL声明如下:

SELECT h.idhighsite AS idhighsite, h.number AS number, h.name AS name, i.ip AS mainbridge, b.branch AS branch, d.district AS district, a.area AS area, h.repeaterheight AS height,
       h.contract AS contract, h.eiastatus AS eia, COUNT(c.idhscontact) AS contactcount, 
       
       GROUP_CONCAT(c.contact) AS contacts,
       GROUP_CONCAT(c.isexisting) AS isexisting,
       GROUP_CONCAT(c.cellid) AS cellid,
       
       CASE WHEN SUBSTRING_INDEX(isexisting,',',1) = 1 THEN 
           CASE 
               WHEN SUBSTRING_INDEX(cellid,',',1) = 1 THEN cu.name1
               WHEN SUBSTRING_INDEX(cellid,',',1) = 2 THEN cu.name2
               WHEN SUBSTRING_INDEX(cellid,',',1) = 3 THEN cu.name3
           END 
       ELSE 
           SUBSTRING_INDEX(contacts,',',1) /* <--- This Fails*/
       END AS name1
       
FROM wmdevl.highsite h 

LEFT JOIN wmdevl.hsitem i ON (i.idhighsite = h.idhighsite AND i.mainbridge = True)
LEFT JOIN wmdevl.branch b ON (b.idbranch = h.idbranch)
LEFT JOIN wmdevl.district d ON (d.iddistrict = h.iddistrict)
LEFT JOIN wmdevl.area a ON (a.idarea = h.idarea)
LEFT JOIN wmdevl.hscontact c ON (c.idhighsite = h.idhighsite) 
LEFT JOIN wmdevl.customer cu ON (cu.idcustomer = c.idcustomer)

GROUP BY h.idhighsite;

当我注释掉有问题的“SUBSTRING_INDEX(contacts,',',1)”时,它就起作用了,并且确实在输出中显示了该列。

感谢@Akina 让我走上了正确的方向,子查询才起作用:

SELECT *,

   CASE WHEN SUBSTRING_INDEX(isexisting,',',1) = 1 THEN 
       CASE 
           WHEN SUBSTRING_INDEX(cellid,',',1) = 1 THEN cuname1
           WHEN SUBSTRING_INDEX(cellid,',',1) = 2 THEN cuname2
           WHEN SUBSTRING_INDEX(cellid,',',1) = 3 THEN cuname3
       END 
   ELSE 
       SUBSTRING_INDEX(contacts,',',1) 
   END AS name1,
   
   CASE WHEN SUBSTRING_INDEX(isexisting,',',1) = 1 THEN 
       CASE 
           WHEN SUBSTRING_INDEX(cellid,',',1) = 1 THEN cucell1
           WHEN SUBSTRING_INDEX(cellid,',',1) = 2 THEN cucell2
           WHEN SUBSTRING_INDEX(cellid,',',1) = 3 THEN cucell3
       END 
   ELSE 
       SUBSTRING_INDEX(numbers,',',1) 
   END AS number1,
   
   CASE WHEN contactcount >= 2 THEN
       CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(isexisting,',',2), ',', -1) = 1 THEN 
           CASE 
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',2), ',', -1) = 1 THEN cuname1
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',2), ',', -1) = 2 THEN cuname2
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',2), ',', -1) = 3 THEN cuname3
           END 
       ELSE 
           SUBSTRING_INDEX(SUBSTRING_INDEX(contacts,',',2), ',', -1)
       END 
   END AS name2,

   CASE WHEN contactcount >= 2 THEN
       CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(isexisting,',',2), ',', -1) = 1 THEN 
           CASE 
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',2), ',', -1) = 1 THEN cucell1
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',2), ',', -1) = 2 THEN cucell2
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',2), ',', -1) = 3 THEN cucell3
           END 
       ELSE 
           SUBSTRING_INDEX(SUBSTRING_INDEX(numbers,',',2), ',', -1) 
       END 
   END AS number2,
   
   CASE WHEN contactcount >= 3 THEN
       CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(isexisting,',',3), ',', -1) = 1 THEN 
           CASE 
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',3), ',', -1) = 1 THEN cuname1
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',3), ',', -1) = 2 THEN cuname2
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',3), ',', -1) = 3 THEN cuname3
           END 
       ELSE 
           SUBSTRING_INDEX(SUBSTRING_INDEX(contacts,',',3), ',', -1)
       END 
   END AS name3,

   CASE WHEN contactcount >= 3 THEN
       CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(isexisting,',',3), ',', -1) = 1 THEN 
           CASE 
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',3), ',', -1) = 1 THEN cucell1
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',3), ',', -1) = 2 THEN cucell2
               WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(cellid,',',3), ',', -1) = 3 THEN cucell3
           END 
       ELSE 
           SUBSTRING_INDEX(SUBSTRING_INDEX(numbers,',',3), ',', -1) 
       END 
   END AS number3
       
FROM (SELECT h.idhighsite AS idhighsite, h.number AS number, h.name AS name, i.ip AS mainbridge, b.branch AS branch, d.district AS district, a.area AS area, h.repeaterheight AS height,
    h.contract AS contract, h.eiastatus AS eia, cu.name1 AS cuname1, cu.name2 AS cuname2, cu.name3 AS cuname3, cu.cell1 AS cucell1, cu.cell2 AS cucell2, cu.cell3 AS cucell3,
    h.notes AS notes, h.caastatus AS caastatus, h.anticollight AS anticollight, h.contractdate AS contractdate, h.evergreen AS evergreen, h.rentescallation AS escallationdate, h.rentamount AS rent,

    COUNT(c.idhscontact) AS contactcount, 

    GROUP_CONCAT(c.contact) AS contacts,
    GROUP_CONCAT(c.number) AS numbers,
    GROUP_CONCAT(c.isexisting) AS isexisting,
    GROUP_CONCAT(c.cellid) AS cellid

    FROM highsite h 

    LEFT JOIN hsitem i ON (i.idhighsite = h.idhighsite AND i.mainbridge = True)
    LEFT JOIN branch b ON (b.idbranch = h.idbranch)
    LEFT JOIN district d ON (d.iddistrict = h.iddistrict)
    LEFT JOIN area a ON (a.idarea = h.idarea)
    LEFT JOIN hscontact c ON (c.idhighsite = h.idhighsite) 
    LEFT JOIN customer cu ON (cu.idcustomer = c.idcustomer)

    GROUP BY h.idhighsite) 
AS x;