MOZ
MOZ

Reputation: 758

MySQL Select query using IF, SUM & Multiplication

I am trying to make a query and display data of a table using if, SUM and multiplication in a single query, orginally what i intend to do is;

Select column1, column2, column3, IF (column1='cr') THEN (column1='cr') * 8 + 
IF (column1='config') THEN (column1='config') * 6 + 
IF (column2='bs' AND column3='ft') THEN (column3='ft') * 4 + 
IF (column2='bs' AND column3='mj') THEN (column3='mj') * 2 + 
IF (column2='bs' AND column3='md') THEN (column3='md') * 5 + 
IF (column2='bs' AND column3='mi') THEN (column3='mi') * 3 + 
IF (column2='lf' AND column3='ft') THEN (column3='ft') * 2 + 
IF (column2='lf' AND column3='mj') THEN (column3='mj') * 1 FROM tbl_ut

Above query is wrong, its just to show what i intend to do. I need help to correct this query. IF works as IF(condition, value if true, value if false)

What other ways are available to correct this query?

Regards

Upvotes: 1

Views: 246

Answers (1)

juergen d
juergen d

Reputation: 204784

Select column1, column2, column3, 
       IF (column1='cr', column1 * 8, 0) + 
       IF (column1='config', column1 * 6, 0) + 
       IF (column2='bs' AND column3='ft', column3 * 4, 0) + 
       IF (column2='bs' AND column3='mj', column3 * 2, 0) + 
       IF (column2='bs' AND column3='md', column3 * 5, 0) + 
       IF (column2='bs' AND column3='mi', column3 * 3, 0) + 
       IF (column2='lf' AND column3='ft', column3 * 2, 0) + 
       IF (column2='lf' AND column3='mj', column3 * 1, 0) 
FROM tbl_ut

Upvotes: 1

Related Questions