2012-05-09 37 views
5
SELECT * 
FROM Activity AA 
WHERE AA.act_id IN 
((SELECT A.act_id 
    FROM Activity A 
    WHERE A.user_id = 'lhfcws') 
UNION 
(SELECT J.act_id 
    FROM Joinin J 
    WHERE J.user_id = 'lhfcws')) 
ORDER BY AA.act_time 

错误消息: #1064 - 你在你的SQL语法错误;检查对应于你的MySQL服务器版本的手册正确的语法使用 附近'UNION (SELECT J.act_id FROM Joinin J WHERE J.user_id = 'lhfcws')) ORDE' at line 7MySQL在UNION附近出现语法错误?

活动(act_id,USER_ID,_名称)
Joinin(act_id,USER_ID)

+0

可能重复的[MySQL的3.23 UNION失败,错误1064](http://stackoverflow.com/questions/9202667/mysql-3-23-union-fails-with-error-1064) – Makoto

+0

重复的没有一个令人满意的[接受]答案;这个呢。 – 2012-05-09 08:26:39

回答

6

原因你的错误是周围的select语句的括号。你应该把它写成:

SELECT * 
FROM Activity AA 
WHERE AA.act_id IN 
(SELECT A.act_id 
    FROM Activity A 
    WHERE A.user_id = 'lhfcws' 
UNION 
SELECT J.act_id 
    FROM Joinin J 
    WHERE J.user_id = 'lhfcws') 
ORDER BY AA.act_time 

但是,你可以去看看@RaphaëlAlthaus改进你的查询的想法。

+0

明白了! thx为你的帮助〜! – Lhfcws

2

嗯,不想你需要这样的子查询

select * from Activity a 
where a.user_id = 'lhfcws' 
and exists (select null from Joinin j 
where a.user_id = j.user_id); 

做前人的精力同样

也许你需要一个更多的检查

select * from Activity a 
    where a.user_id = 'lhfcws' 
    and exists (select null from Joinin j 
    where a.user_id = j.user_id 
    and a.act_id = j.act_id); 

Acording到@Jonathan莱弗勒的(真正)备注

select * from Activity a 
     where a.user_id = 'lhfcws' 
     or exists (select null from Joinin j 
     where j.user_id = 'lhfcws' 
     and a.act_id = j.act_id); 
+0

UNION是一个OR;你是指OR EXISTS? –

+0

哎呀,你说得对,thx,第三版似乎对你正确吗? –

+0

哇〜!它的作品! thx很多~~但我仍然想知道为什么我错了~~ – Lhfcws