(我已经查过了,找不到答案,如果这是重复的,只需指出一个链接,我将删除此问题)
假设我有以下代码
select CASE WHEN lower(Http_User_Agent) like '%mobi%' then 'Mobile'
else 'Desktop' end as Device
from Http_User_Agent
where Device = 'Mobile'
Run Code Online (Sandbox Code Playgroud)
哪个返回
Msg 207, Level 16, State 1, Line 4
Invalid column name 'Device'.
Run Code Online (Sandbox Code Playgroud)
我基本上要做的是按新创建的 column 进行过滤Device。有没有简单的方法?
我在 Windows 7 上使用 SQL Server 2008
在执行查询时,Device别名尚未分配给结果集。所以你需要这样做:
SELECT
CASE WHEN LOWER(Http_User_Agent) LIKE '%mobi%' THEN 'Mobile'
ELSE 'Desktop'
END AS Device
FROM Http_User_Agent
WHERE
CASE WHEN LOWER(Http_User_Agent) LIKE '%mobi%' THEN 'Mobile'
ELSE 'Desktop'
END = 'Mobile'
Run Code Online (Sandbox Code Playgroud)
...即根本不引用新别名。或这个
SELECT *
FROM
(SELECT
CASE WHEN LOWER(Http_User_Agent) LIKE '%mobi%' THEN 'Mobile'
ELSE 'Desktop'
END AS Device
FROM Http_User_Agent)
WHERE Device = 'Mobile'
Run Code Online (Sandbox Code Playgroud)
...即从周围的选择中引用它