我有一个以下形式的mysql表:
id valA valB
1 0 0
2 0 1
3 1 0
4 1 1
And it goes on like that..
属性 valA 和 valB 只有布尔值。我想找出一个mysql查询,它将产生以下输出:
id result
1 A
2 B
3 C
4 D
这里使用的逻辑是:
if valA = 0 and valB = 0 then result = A
if valA = 0 and valB = 1 then result = B
if valA = 1 and valB = 0 then result = C
if valA = 1 and valB = 1 then result = D
我发现这有点类似question,但未能将其与此相关联。
提前感谢您的帮助。
答案 0 :(得分:1)
这样的东西?
select
case
when valA = 0 and valB = 0 then 'A'
when valA = 0 and valB = 1 then 'B'
when valA = 1 and valB = 0 then 'C'
when valA = 1 and valB = 1 then 'D'
end as result
from
table
答案 1 :(得分:0)
select id,case when valA = 0 and valB = 0 then 'A' when valA = 0 and valB = 1 then 'B' when valA = 1 and valB = 0 then 'C' when valA = 1 and valB = 1 then 'D' end as result from table1
答案 2 :(得分:0)
如果在二进制系统中将valA和valB作为两位,则在将它们转换为十进制后,可以将结果用作某些列表的索引。
SELECT id, ELT((2*valA+valB+1),'A','B','C','D') AS my_result FROM table