2012-05-09 36 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