各位老大,帮忙看看这个嵌套查询是怎么回事:
其中有关的表:
task(task_id int,text varchar(200))
action(belong2task int,text varchar(200))
做如下嵌套查询时报告语法错误:
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 'union
(select Belong2Task
from WWHAction
where Text like '%as%'
)' at line 9sql语句是:
select *
from Task
where Task_ID in
(
(select Task_ID
from Task
where Text like '%as%'
)
union
(select Belong2Task
from Action
where Text like '%as%'
)
);
其中有关的表:
task(task_id int,text varchar(200))
action(belong2task int,text varchar(200))
做如下嵌套查询时报告语法错误:
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 'union
(select Belong2Task
from WWHAction
where Text like '%as%'
)' at line 9sql语句是:
select *
from Task
where Task_ID in
(
(select Task_ID
from Task
where Text like '%as%'
)
union
(select Belong2Task
from Action
where Text like '%as%'
)
);
from `Task`
where Task_ID in
(
(select Task_ID
from `Task`
where `Text` like '%as%'
)
union
(select Belong2Task
from `Action`
where `Text` like '%as%'
)
)
text 应该是保留字。
from `Task`
where Task_ID in
(
(select Task_ID
from `Task`
where `Text` like '%as%'
) );这是可以通过的,而且可以查询出结果来,换成Action表也一样,就是加上union后不行。
因此应该和Text是否保留字没有关系。
难道in里不能嵌套union?
from Task
where Task_ID in
(
select Task_ID
from Task
where Text like '%as%'
union
select Belong2Task
from Action
where Text like '%as%'
);
括号加的太多了。
FROM Task
WHERE Task_ID IN
(
SELECT Task_ID
FROM Task
WHERE `Text` LIKE '%as%'
UNION
SELECT Belong2Task
FROM `Action`
WHERE `Text` LIKE '%as%'
);orSELECT a.* FROM Task a
INNER JOIN
(
SELECT Task_ID
FROM Task
WHERE `Text` LIKE '%as%'
UNION
SELECT Belong2Task
FROM `Action`
WHERE `Text` LIKE '%as%'
) b
ON a.Task_ID=b.Task_ID