在 SQL Select 语句中使用 If else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13526592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using If else in SQL Select statement
提问by user160820
I have a select statement which will return 2 columns.
我有一个 select 语句,它将返回 2 列。
ID | IDParent
Then in my program I have to test if IDParent is < 1 then use ID ELSE use IDParent
然后在我的程序中我必须测试 if IDParent is < 1 then use ID ELSE use IDParent
Is there a way to use an If Else like condition in SQL to return only single value?
有没有办法在 SQL 中使用 If Else 之类的条件来仅返回单个值?
回答by John Woo
you can use CASE
您可以使用 CASE
SELECT ...,
CASE WHEN IDParent < 1 THEN ID ELSE IDPArent END AS ColumnName,
...
FROM tableName
回答by jainvikram444
Here, using CASE Statement
and find result:
在这里,使用CASE Statement
并找到结果:
select (case when condition1 then result1
when condition2 then result2
else result3
end) as columnname from tablenmae:
For example:
例如:
select (CASE WHEN IDParent< 1 then ID
else IDParent END) as columnname
from tablenmae
回答by AnandPhadke
SELECT CASE WHEN IDParent < 1
THEN ID
ELSE IDParent
END AS colname
FROM yourtable
回答by user4112005
The query will be like follows
查询将如下所示
SELECT (CASE WHEN tuLieuSo is null or tuLieuSo=''
THEN 'Ch?a có ??a'
ELSE 'Có ??a' End) AS tuLieuSo,moTa
FROM [gPlan_datav3_SQHKTHN].[dbo].[gPlan_HoSo]
回答by user3380585
sql server 2012
sql server 2012
with
student as
(select sid,year from (
values (101,5),(102,5),(103,4),(104,3),(105,2),(106,1),(107,4)
) as student(sid,year)
)
select iif(year=5,sid,year) as myCol,* from student
myCol sid year
101 101 5
102 102 5
4 103 4
3 104 3
2 105 2
1 106 1
4 107 4
回答by Rohit Mewada
You can use simply if
statement under select
query as like I have described below
您可以if
在select
查询下使用简单的语句,就像我在下面描述的那样
if(some_condition,if_satisfied,not_satisfied)
if(some_condition,if_satisfied,not_satisfied)
SELECT IF(IDParent < 1,ID,IDParent) FROM yourTable ;
回答by Phan Van Linh
Here is some example using CASE WHEN
这是使用的一些示例 CASE WHEN
SELECT
CASE WHEN A > 1 THEN
'Greater than 1'
END
FROM TRIANGLES
.
.
SELECT
CASE WHEN A > 1 THEN
A
END
FROM TRIANGLES
.
.
SELECT
CASE WHEN A > 1 and B > 1 THEN
'Greater than 1'
END
FROM TRIANGLES
.
.
SELECT
CASE WHEN A > 1 THEN
'greater than 1'
ELSE
'less than 1'
END
FROM TRIANGLES
.
.
SELECT
CASE WHEN A > 1 THEN
'greater than 1'
ELSE CASE WHEN A >= 0 THEN
'greater than or equal 0'
ELSE
'less than 0'
END
END
FROM TRIANGLES;
Hope this helps
希望这可以帮助
回答by Davoud Gharajeh
I Have a Query With This result :
我有一个查询结果:
SELECT Top 3
id,
Paytype
FROM dbo.OrderExpresses
WHERE CreateDate > '2018-04-08'
The Result is :
结果是:
22082 1
22083 2
22084 1
I Want Change The Code To String In Query, So I Use This Code :
我想在查询中将代码更改为字符串,因此我使用此代码:
SELECT TOP 3
id,
CASE WHEN Paytype = 1 THEN N'Credit' ELSE N'Cash' END AS PayTypeString
FROM dbo.OrderExpresses
WHERE CreateDate > '2018-04-08'
And Result Is :)
结果是:)
22082 Credit
22083 Cash
22084 Credit
回答by OkieOth
You can also use a union construct. I'm not sure if CASE is a common SQL construct ...
您还可以使用联合构造。我不确定 CASE 是否是一个常见的 SQL 构造......
SELECT ID FROM tabName WHERE IDParent<1 OR IDParent IS NULL
UNION
SELECT IDParent FROM tabName WHERE IDParent>1
回答by Nirav Ranpara
select
CASE WHEN IDParent is < 1 then ID else IDParent END as colname
from yourtable