我想使用SQL来查询我的访问(2010)数据库中的一些数据。
我想做的是将所有IBS人员拉出来。 (这我知道怎么做)。 然后使用SQL创建一个变量,如果存在“?IBS”(未诊断),则存储0;如果在此变量中存储(诊断为IBS),则存储1。 此信息存储在名为“AdditionalHealthProblems”的文本变量中。
我正在尝试使用带有Like的SELECT CASE语句,但似乎无法使其工作。
SELECT AdditionalHealthProblems, 'Diagnosed' =
CASE
WHEN AdditionalHealthProblems LIKE '*?IBS*' THEN '0'
ELSE '1'
END
FROM tblFollowUpQs
WHERE (((tblFollowUpQs.AdditionalHealthProblems) Like '*IBS*'));
但这不起作用。我通过错误消息在“'Diagnosed' = CASE WHEN AdditionalHealthProblems LIKE '*?IBS*' THEN '0' ELSE '1'END"
语句中收到错误。
有人可以告诉我有什么问题吗? 或者,如果有更好的方法来获取此信息?
谢谢!
----------------- EDIT --------------------
已更改为
SELECT tblFollowUpQs.AdditionalHealthProblems, CASE
WHEN tblFollowUpQs.AdditionalHealthProblems LIKE 'IBS' THEN '0' END As Diagnosed
FROM tblFollowUpQs
WHERE (((tblFollowUpQs.AdditionalHealthProblems) Like 'IBS'));
但它还没有用......
错误消息提供语法错误并突出显示WHEN
--------------- ANSWER --------------------------
将选择案例切换到Switch()函数并且它有效!
这是最终的代码:
SELECT tblFollowUpQs.AdditionalHealthProblems, Switch( tblFollowUpQs.AdditionalHealthProblems LIKE '*[?]*', 1, tblFollowUpQs.AdditionalHealthProblems NOT LIKE '*[?]*', 0)
FROM tblFollowUpQs
WHERE (((tblFollowUpQs.AdditionalHealthProblems) Like '*IBS*'));
答案 0 :(得分:5)
Microsoft Access不支持CASE...WHEN
语句。请改用IIF
语句:
IIF(表达,值如果为真,值如果为)
SELECT tblFollowUpQs.AdditionalHealthProblems,
IIf(tblFollowUpQs.AdditionalHealthProblems Like "IBS",1,0) AS [Diagnosed]
FROM tblFollowUpQs
WHERE (((tblFollowUpQs.AdditionalHealthProblems) Like "'IBS"));
如果您需要使用通配符,可以在office.microsoft.com
上阅读更多内容答案 1 :(得分:0)
<强>查询强>
SELECT AdditionalHealthProblems, Diagnosed= (
CASE
WHEN AdditionalHealthProblems LIKE '?IBS' THEN '0'
ELSE '1'
END)
FROM tblFollowUpQs
WHERE (((tblFollowUpQs.AdditionalHealthProblems) Like '%IBS'));