如何从多列中获取
How to get from multiple column
我有以下 table 数据:
id send_user_id to_user_id status
1 5 56 0
2 5 125 1
3 500 5 1
4 850 5 0
5 5 365 0
6 5 800 1
7 8520 156 1
现在我想得到 send_user_id =5 or to_user_id = 5 and status = 1
我试过以下查询
select * from mtable
where send_user_id = 5 and status = 1 or to_user_id = 5 and status = 1
通过以上我得到以下数据:
id send_user_id to_user_id status
2 5 125 1
3 500 5 1
6 5 800 1
但我想要以下输出 :
all_user_id
125
500
800
使用 CASE 表达式:
select case when send_user_id = 5 then to_user_id else send_user_id end all_user_id
from mtable
where 5 in (send_user_id, to_user_id) and status = 1
另一种可行的方法是因为我们正在处理整数列:
select send_user_id + to_user_id - 5 all_user_id
from mtable
where 5 in (send_user_id, to_user_id) and status = 1
参见demo。
结果:
> | all_user_id |
> | ----------: |
> | 125 |
> | 500 |
> | 800 |
我有以下 table 数据:
id send_user_id to_user_id status
1 5 56 0
2 5 125 1
3 500 5 1
4 850 5 0
5 5 365 0
6 5 800 1
7 8520 156 1
现在我想得到 send_user_id =5 or to_user_id = 5 and status = 1
我试过以下查询
select * from mtable
where send_user_id = 5 and status = 1 or to_user_id = 5 and status = 1
通过以上我得到以下数据:
id send_user_id to_user_id status
2 5 125 1
3 500 5 1
6 5 800 1
但我想要以下输出 :
all_user_id
125
500
800
使用 CASE 表达式:
select case when send_user_id = 5 then to_user_id else send_user_id end all_user_id
from mtable
where 5 in (send_user_id, to_user_id) and status = 1
另一种可行的方法是因为我们正在处理整数列:
select send_user_id + to_user_id - 5 all_user_id
from mtable
where 5 in (send_user_id, to_user_id) and status = 1
参见demo。
结果:
> | all_user_id |
> | ----------: |
> | 125 |
> | 500 |
> | 800 |