您好我想要检索用户ID
使用的浏览器列表表格包含
UserID int
BrowserName nvarchar(40)
这是我的查询
select browser =
CASE
WHEN ( PATINDEX('%IE%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8)
WHEN (PATINDEX('%Firefox%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Firefox%',BrowserName),8)
WHEN (PATINDEX('%Chrome%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Chrome%',BrowserName),6)
END
from tableBrowsers where UserId =21
但是如何在此查询中仅选择前1个子字符串。
例如:在case case语句之后我只需要为该情况返回一行,我试过这个,但是不知道如何实现以防万一
THEN select top 1 SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8) from browsertable
输出将是这样的
IE
FIREFOX
CHROME
如果用户使用了三个浏览器
答案 0 :(得分:1)
只需添加DISTINCT:
SELECT DISTINCT browser =
CASE
WHEN ( PATINDEX('%IE%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8)
WHEN (PATINDEX('%Firefox%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Firefox%',BrowserName),8)
WHEN (PATINDEX('%Chrome%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Chrome%',BrowserName),6)
END
FROM tableBrowsers where UserId =21