2014-02-22 49 views
0

我創建了一個名爲tbl的表別名,我想從中選擇。但我無法做到這一點。我知道我的代碼不正確,也沒有優化,但我只是測試MySQL CASE是否可以從mysql中的別名中進行選擇?

select 
case 
when exists (select username from tbl) then 'Username Exists' 
else 'Username does not exist' 
end 
from (select 1 as id, 'bob' as username, 'pass' as password) as tbl 

我收到錯誤:Table 'users.tbl' doesn't exist in database users

+0

您沒有在SQL查詢中使用任何物理表!你有表名爲[tbl]還是數據庫表的同義詞? – Agilox

回答

3

,因爲有查詢中涉及的任何物理表您有一個錯誤,因爲tbl正是您所創建的別名。 如果您只想測試您的用戶名是否存在,請執行此查詢:

SELECT CASE 
WHEN id = 1 THEN 'Username Exists' ELSE 'Username does not exist' 
END 
FROM (SELECT 1 AS id, 'bob' AS username, 'pass' AS password) AS tbl 
1

嘗試了這一點

select 
    case 
     when exists (select username from tbl where username = 'bob' and password = 'pass') then 'Username Exists' 
    else 'Username does not exist' 
    end as existanse_column 
from tbl 
limit 1 

DEMO HERE

+0

'tbl'不是一個物理查詢。如果是這樣,你的查詢可以工作,但這不是這種情況。 –

+0

@AlbertoSolano檢查我的演示。 –

相關問題