嵌套查询无效

时间:2016-07-01 05:23:16

标签: php mysql sql

错误未知列'状态'在'字段列表'但是状态列在数据库表中可用。

//query
SELECT `country` ,GROUP_CONCAT(`state` separator ",") as a
FROM (

SELECT `country` , CONCAT( `state` , ':', GROUP_CONCAT( DISTINCT `district`
ORDER BY `district` ASC
SEPARATOR ',' ) ) AS NAME
FROM `temp_location`
GROUP BY `country` , `state`
) AS result
GROUP BY `country`

是否有其他方法可以解决此问题?

3 个答案:

答案 0 :(得分:2)

您的子查询不会返回列state,只返回列countryNAME

答案 1 :(得分:2)

您的子查询中没有state列,请尝试此操作;)

SELECT `country` ,GROUP_CONCAT(`NAME` separator ",") as a
FROM (

SELECT `country` , CONCAT( `state` , ':', GROUP_CONCAT( DISTINCT `district`
ORDER BY `district` ASC
SEPARATOR ',' ) ) AS NAME
FROM `temp_location`
GROUP BY `country` , `state`
) AS result
GROUP BY `country`

<强> 被修改

select `country` ,GROUP_CONCAT(`state` SEPARATOR "//" ) AS `a`
from (
    select
        `country`,
        concat(`state`, ":", GROUP_CONCAT(`district` separator ",")) as `state`
    from `temp_location` 
    group by `country`, `state`) `result`
group by `country`

答案 2 :(得分:2)

您的子查询错过了列名state,尝试将NAME更改为state。检查以下更新的查询,

    SELECT `country` ,GROUP_CONCAT(`state` separator ",") as a
        FROM (SELECT `country`, CONCAT(`state` , ':', 
            GROUP_CONCAT(DISTINCT `district` ORDER BY `district` ASC 
               SEPARATOR ',')) AS `state`
            FROM `temp_location` GROUP BY `country`, `state`) AS result
        GROUP BY `country`