I have a select statement which will return 2 columns.
我有一个select语句,它将返回两列。
ID | IDParent
Then in my program I have to test if IDParent is < 1 then use ID ELSE use IDParent
然后在我的程序中,我必须测试IDParent是否< 1,然后使用ID ELSE使用IDParent。
Is there a way to use an If Else like condition in SQL to return only single value?
是否有一种方法可以使用SQL中的If条件只返回单个值?
8 个解决方案
#1
65
you can use CASE
你可以用例
SELECT ...,
CASE WHEN IDParent < 1 THEN ID ELSE IDPArent END AS ColumnName,
...
FROM tableName
#2
19
SELECT CASE WHEN IDParent < 1
THEN ID
ELSE IDParent
END AS colname
FROM yourtable
#3
17
Here, using CASE Statement
and find result:
在这里,用CASE语句找到结果:
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
#4
4
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]
#5
1
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
#6
0
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
#7
0
select
CASE WHEN IDParent is < 1 then ID else IDParent END as colname
from yourtable
#8
0
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 :
我想在查询中将代码改为String,所以我使用了以下代码:
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
#1
65
you can use CASE
你可以用例
SELECT ...,
CASE WHEN IDParent < 1 THEN ID ELSE IDPArent END AS ColumnName,
...
FROM tableName
#2
19
SELECT CASE WHEN IDParent < 1
THEN ID
ELSE IDParent
END AS colname
FROM yourtable
#3
17
Here, using CASE Statement
and find result:
在这里,用CASE语句找到结果:
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
#4
4
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]
#5
1
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
#6
0
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
#7
0
select
CASE WHEN IDParent is < 1 then ID else IDParent END as colname
from yourtable
#8
0
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 :
我想在查询中将代码改为String,所以我使用了以下代码:
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