| |

VerySource

 Forgot password?
 Register
Search
View: 632|Reply: 2

A tricky SQL statement, ask prawn for help

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-9 19:00:01
| Show all posts |Read mode
There are three fields a, b, and c in the table, where the value of field a is taken when c = 0, and the value of field b is taken when c = 1, how to write such a SQL statement? Thank you
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-11 01:00:01
| Show all posts
select (case when c = 0 then a else b end) as val from table
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-11 04:09:02
| Show all posts
create table tablename (
a char (10),
b char (10),
c int)

insert into tablename
select 'aa', 'bb', 0
union all
select 'cc', 'dd', 1

select tablename. *, value = case when c = 0 then a else b end from tablename

result:
a b c value
aa bb 0 aa
cc dd 1 dd
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list