問題描述
這可能不是有效的 SQL,但這是我想要做的:
This probably isn't valid SQL, but this is what I'm trying to do:
SELECT *
FROM tablename
WHERE
(CASE @ScoreType
WHEN 1 THEN score1
WHEN 2 THEN score2
ELSE score3 END) >= @Score
基本上,我試圖根據 @ScoreType
的值在 WHERE
子句中按不同的列進行過濾.我猜測我的方法無效是否正確?如果是這樣,解決此問題的正確方法是什么?
Basically, I'm trying to filter by different columns in the WHERE
clause depending on the value of @ScoreType
. Am I right in guessing that my approach isn't valid? If so, what would be the proper way to go about this?
edit:抱歉,我在簡化查詢時不小心省略了 end
以在此處發布.實際查詢確實有 end
.查詢本身運行良好,但結果并不如預期.原來問題出在別處.我懷疑 WHERE
子句,因為我以前從未嘗試過這樣的事情,并且不確定它是否是有效的 SQL.
edit: Sorry, I accidentally omitted end
while simplifying the query to post here. The actual query does have end
. The query itself runs fine, but the results weren't as expected. It turns out the issue was elsewhere. I suspected the WHERE
clause because I've never tried anything like this before and was unsure of whether it was valid SQL.
推薦答案
這應該可行嗎?
SELECT *
FROM tablename
WHERE
(CASE @ScoreType
WHEN 1 THEN score1
WHEN 2 THEN score2
WHEN 3 THEN score3
END) >= @Score
<小時>
我認為問題是:你忘記了案例的end
I think the problem is: you forgot the end
to the case
這篇關于結合使用 CASE 和 WHERE 來指定不同的列作為過濾依據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!