2012-06-08 80 views
1

SQL查詢:MySQL的比較列,子查詢

select 
usr.username, 
(
    select usr2.username 
    from users as usr2 
) as another_user 
from users as usr 
having usr.username != another_user 

我怎麼能比較usr.username與命名爲another_user例如子查詢?

我想要得到的所有結果,其中usr.username不等於another_user

我試過WHERE條款或HAVING,但仍然得到錯誤

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax 
+0

你可以填寫更多的查詢嗎?這些括號之間插入了什麼? –

+0

我只是從同一個表中獲得另一個用戶 – fxuser

+0

忘記sql一分鐘......你究竟想完成什麼?如果你告訴我們,我們可以幫助你更好,因爲你的查詢看起來有點奇怪 - 目前還不清楚你想要做什麼。 – Bohemian

回答

0

嘗試

select 
usr.username, 
(
    select username from users where ... 
) as another_user 
from users as usr 
where usr.username <> another_user.username 
0

也許像這樣:

SELECT 
    * 
FROM 
(
    select 
    usr.username, 
    (
     .... 
    ) as another_user 
    from users as usr 
) AS T 
WHERE 
    NOT T.username = T.another_user