以下查询返回结果:
SELECT id_a,
id_b,
id_c
FROM (
SELECT id_a,
id_b,
id_c,
Dep_Travail_b
FROM (
SELECT A.ID id_a,
B.ID id_b,
B.Dep_Travail Dep_Travail_b
FROM [regal-cider-369:insee_recensement_1968_2010.Dep_Naissance] A
INNER JOIN [regal-cider-369:insee_recnsement_1968_2010.Dep_Travail] B
ON A.ID = B.ID
) as JOIN1
INNER JOIN (
SELECT ID id_c
FROM [regal-cider-369:insee_recnsement_1968_2010.Dep_Residence] C
) as C
ON id_a = id_c
)
ORDER BY id_a,
id_b,
id_c
但如果ORDER BY子句被GROUP BY替换,则返回以下错误:
找不到字段'id_c';你是说'id_a'吗?
相同查询的简化版本导致相同的结果(在查询结束时使用ORDER BY,使用GROUP BY失败)。
SELECT id_a,
id_b,
id_c
FROM (
SELECT A.ID id_a ,
B.ID id_b
FROM [regal-cider-369:insee_recensement_1968_2010.Dep_Naissance] A
INNER JOIN [regal-cider-369:insee_recensement_1968_2010.Dep_Travail] B
ON A.ID = B.ID
) as JOIN1
INNER JOIN (
SELECT ID id_c
FROM [regal-cider-369:insee_recensement_1968_2010.Dep_Residence] C
) as C
ON id_a = id_c
ORDER BY id_a,
id_b,
id_c
GROUP BY和加入是否有问题,或者我错过了什么?
答案 0 :(得分:1)
当我为所选字段添加别名(字段,字段,字段)时,它似乎正在工作, 与GROUP BY一起使用的简单版本是:
SELECT id_a AS fieldA,
id_b as fieldB,
id_c as fieldC
FROM (
SELECT A.ID id_a ,
B.ID id_b
FROM [regal-cider-369:insee_recensement_1968_2010.Dep_Naissance] A
INNER JOIN [regal-cider-369:insee_recensement_1968_2010.Dep_Travail] B
ON A.ID = B.ID
) as JOIN1
INNER JOIN (
SELECT ID id_c
FROM [regal-cider-369:insee_recensement_1968_2010.Dep_Residence] C
) as C
ON id_a = id_c
GROUP BY fieldA,
fieldB,
fieldC