我正在尝试从我的主查询中加入users_groups,但我似乎无法在别名表上加入它们。请帮我。它导致错误:'on clause'
中的未知列'node.id'SELECT node.id, node.first_name, node.last_name, (COUNT( parent.id ) - ( sub_tree.depth +1 ) ) AS depth
FROM users AS node, users AS parent, users AS sub_parent, (
SELECT node.id, node.first_name, node.last_name, (COUNT( parent.id ) -1) AS depth
FROM users AS node, users AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id =1
GROUP BY node.id
ORDER BY node.lft
) AS sub_tree
JOIN users_groups ON users_groups.user_id = node.id
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.id = sub_tree.id
GROUP BY node.id
ORDER BY node.lft
LIMIT 0 , 30
答案 0 :(得分:3)
格式化SQL
始终有用
SELECT
node.id,
node.first_name,
node.last_name,
( COUNT( parent.id ) - ( sub_tree.depth +1 ) ) AS depth
FROM
users AS node,
users AS parent,
users AS sub_parent,
(
SELECT
node.id,
node.first_name,
node.last_name,
( COUNT( parent.id ) -1 ) AS depth
FROM
users AS node,
users AS parent
WHERE
node.lft BETWEEN parent.lft AND parent.rgt
AND node.id =1
GROUP BY
node.id
ORDER BY
node.lft
) AS sub_tree
JOIN users_groups ON users_groups.user_id = node.id
^^ should be sub_tree.id
WHERE
node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.id = sub_tree.id
GROUP BY
node.id
ORDER BY
node.lft
LIMIT 0 , 30
答案 1 :(得分:2)
您必须使用您提供的别名..
JOIN users_groups ON users_groups.user_id = sub_tree.id