select distinct id, item from
(
(
(
select ID as id, "INNER" as item
from TREE
where ID in
(
select P_ID from TREE as T1
)
and P_ID is not null
)
as T3
)
union
(
(
select ID as id, "ROOT" as item
from TREE
where P_ID IS NULL
)
as T2
)
)
TREE 表的结构如下:
ID, P_ID
1,2
3,4
5,6
我是个SQL小白,真不知道为什么执行的时候总是出问题。
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as T2 ) )' at line 22
你能告诉我吗?谢谢!
最佳答案
不知道您要通过查询实现什么,但可以修复语法错误,您可以将查询重写为
SELECT DISTINCT
id,
item
FROM(
SELECT
ID AS id,
"INNER" AS item
FROM
TREE
WHERE ID IN (SELECT P_ID FROM TREE)
AND P_ID IS NOT NULL
UNION
SELECT
ID AS id,
"ROOT" AS item
FROM
TREE
WHERE P_ID IS NULL
) as t
关于MySQL语法错误,关于union和as关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519487/