Reputation: 6068
I have a table which includes an ENUM field with values 'B', 'N', 'F', and 'V'. I would like to assign each of these letters a weight:
B: -2
N: -1
F: 1
V: 2
These weights should appear in a column in a select statement. Finally, I want to add these values together. Is this possible?
The goal is to do this in django, but using MySQL will also work.
Upvotes: 1
Views: 353
Reputation: 781726
Use a CASE
expression:
select sum(case enum_field
when 'B' then -2
when 'N' then -1
when 'F' then 1
when 'V' then 2
end) as total_sum
from your_table
Upvotes: 1
Reputation: 204854
select sum(case when enum_field = 'B' then -2
when enum_field = 'N' then -1
when enum_field = 'F' then 1
when enum_field = 'V' then 2
end) as total_sum
from your_table
Upvotes: 1