数据库查询失败:字段列表中的列“id”不明确

时间:2013-05-20 07:57:57

标签: mysql

       SELECT  realestate.id,node.id,node.name
                from realestate,
                (SELECT
                    GROUP_CONCAT(parent.name_en ORDER BY parent.lft DESC) AS name,id
                  FROM
                    address AS node JOIN address AS parent
                    ON node.lft BETWEEN parent.lft AND parent.rgt  
                  WHERE node.lft >={$lft} and node.rgt <={$rgt}
                  GROUP BY node.name) as node
        INNER JOIN users ON id_user = users.id
        LEFT JOIN pic ON id_realestate = realestate.id
        WHERE node.id = realestate.address_id

我尝试多次修改它,此错误消息不会消失“数据库查询失败:字段列表中的列'id'不明确”。有什么想法解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

问题在于子查询,

SELECT  GROUP_CONCAT(parent.name_en ORDER BY parent.lft DESC) AS name,
        id  -- <<== HERE (change to node.ID or parent.ID)
FROM    address AS node 
        JOIN address AS parent
           ON node.lft BETWEEN parent.lft AND parent.rg

您应指定ID来自哪个表,因为node and parent包含导致歧义的ID。它可以是node.IDparent.ID

答案 1 :(得分:2)

这是因为您在连接的查询中使用了id,所以它不明确,只需更改以避免错误

GROUP_CONCAT(parent.name_en ORDER BY parent.lft DESC) AS name,node.id --here you need to specify that is from node

答案 2 :(得分:0)

改变这个:

GROUP_CONCAT(parent.name_en ORDER BY parent.lft DESC) AS name,id

GROUP_CONCAT(parent.name_en ORDER BY parent.lft DESC) AS name,node.id

您需要指定表的别名,因为mysql不知道用于获取id列的表(假设两个表都有一个名为id的列)。