为派生 table 选择数据时出现未知列错误

Getting unknown column error when selecting data for a derived table

我需要一个查询来创建一个 table 来计算给定字段中每个值的数量,但是由于这个 table 中的数据不足,不得不加入另一个 table 获得一个附加值 (NName):

Records_table--------------       Name_table---------
Ref    Score     Iteration        Ref      NName
1      High      1                1        Falcon
1      Middle    2                2        Willow
2      Middle    1                3        Lance
2      Middle    2                4        Ranger
2      Low       3
3      Low       1
4      High      1
4      High      2
4      High      3

需要输出:

NName      High  Middle  Low
Falcon     1     1       0
Willow     0     2       1
Lance      0     0       1
Ranger     3     0       0

这是我拥有的: (我注意到我在 SELECT 字段中进行了编辑("Ref")我在我的版本中删除了)

SELECT 
    tc.nname AS NName, 
    COUNT(High) High,
    COUNT(Middle) Middle,
    COUNT(Low) Low
FROM
    (SELECT 
        NName, 'High' High, NULL Middle, NULL Low
    FROM
        records_table tr
        JOIN name_table tc ON tc.ref = tr.ref
    WHERE
        tr.score = 'High'
    UNION ALL 
    SELECT 
       NName, NULL, 'Middle', NULL
   FROM
       records_table tr
        JOIN name_table tc ON tc.ref = tr.ref
   WHERE
       tr.score = 'Middle'
    UNION ALL
    SELECT 
       NName, NULL, NULL, 'Low'
   FROM
       records_table tr
        JOIN name_table tc ON tc.ref = tr.ref
   WHERE
       tr.score = 'Low' ) T 
GROUP BY NName;

失败 "ERROR 1054 (42S22): Unknown column 'NName' in 'field list' "

我做错了什么,我需要做什么才能使它正常工作?

您的 table 别名是 T 而不是 tc 那么您应该在 main select

中使用 T.nname
    SELECT 
            T.`NNAME` AS NName, 
            COUNT(High) High,
            COUNT(Middle) Middle,
            COUNT(Low) Low
    FROM  ( 
        SELECT 
                Ref,`NNAME`, 'High' High, NULL Middle, NULL Low
        FROM  records_table tr
        JOIN name_table tc ON tc.ref = tr.ref
        WHERE tr.score = 'High'
        UNION ALL 
        SELECT 
               Ref,`NNAME`, NULL, 'Middle', NULL
        FROM records_table tr
        JOIN name_table tc ON tc.ref = tr.ref
        WHERE tr.score = 'Middle'
        UNION ALL
        SELECT 
               Ref,`NNAME`, NULL, NULL, 'Low'
        FROM records_table tr
        JOIN name_table tc ON tc.ref = tr.ref
        WHERE tr.score = 'Low' 
    ) T 
    GROUP BY T.`NNAME`;

虽然不是严格意义上的问题(这是一个封闭的问题,@scaisEdge 给出了正确答案),但我认为 union 不是最好的选择。考虑条件聚合

select n.ref,n.nname,
          sum(case when score = 'high' then 1 else 0 end) High,
          sum(case when score = 'middle' then 1 else 0 end) middle,
          sum(case when score = 'low' then 1 else 0 end) low
from n 
join r on r.ref = n.ref
group by n.ref,n.nname;

+------+--------+------+--------+------+
| ref  | nname  | High | middle | low  |
+------+--------+------+--------+------+
|    1 | Falcon |    1 |      1 |    0 |
|    2 | Willow |    0 |      2 |    1 |
|    3 | Lance  |    0 |      0 |    1 |
|    4 | Ranger |    3 |      0 |    0 |
+------+--------+------+--------+------+
4 rows in set (0.00 sec)