Mysql: look up value in two columns and return the colum name he liked
1 answer
First select all rows where a or b is 7. Then use a case statement to return the column name, which is 7.
select id,
case when a = 7
then 'a'
else 'b'
end
from mytable
where 7 in (a,b)
Another way to use union all
select id , 'a'
from mytable where a = 7
union all
select id , 'b'
from mytable where b = 7
If both a and b can be 7 for the same row, the query union all
will return row IDs where both values ββare 7 times for each column, whereas the first query only returns them once with a value a
.
+1
source to share