如何解决如何从多列中获取
我有下表的数据:
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或to_user_id = 5且状态= 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 |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。