如果在其他条件下如何从表中选择特定Id

问题描述:

如何在if else条件中从表中选择特定的id

how can i select perticular id from table in if else condition

而不是IF-ELSE你需要使用CASE [ ^ ]

这是关于主题的CodeProject文章简单使用SQL CASE表达式 [ ^ ]。

Instead of IF-ELSE you need to use CASE[^]
Here is a CodeProject article on the subject A Simple Use of SQL CASE Expression[^].
会员10694442说:
Member 10694442 said:

&我想对所有4000行执行此操作

& i want to do this for all 4000 rows

请注意,如果不使用 WHERE [ ^ ]子句然后它将影响表格上的每一行 - 更多信息 here [ ^ ]。



在你的情况下,这可行:

Be aware that if you don't filter your SQL query using a WHERE[^] clause then it will affect every row on the table - more information here[^].

In your case this would work:

select CASE WHEN [close] > [ref] THEN 'Buy' ELSE 'Sell' END
from YourTable

请注意,我用方括号包围了列名,因为 close 是SQL Server的保留字之一[ ^ ]。最好避免将保留字用作列名或变量名。

Notice that I have surrounded the column names with square brackets because close is one of SQL Server's reserved words[^]. It is best practise to avoid using reserved words as column or variable names.