简体   繁体   中英

MySQL IF Condition in a Calculated Field

I am trying to create a calculated field where if two values happen to be the same, the value doubles. I am coding this condition in the variable MY. I am new to SQL, so I apologize.

Here is my attempt:

SELECT EXPERT_SCORE*MY
IF(CONSUMER_EXPERT_SCORE_ID= CONSUMER_EXPERT_ID, 1, -1) AS MY 
FROM consumer_expert_score
WHERE CONSUMER_EXPERT_SCORE_ID=2 OR 1;

Thanks in advance!

You want CASE WHEN, and the IN ( ) clause.

Unfortunately I could not rewrite your query completely because your conditional is a tautology, it will always return 1. Maybe you meant something else? Anyway, I changed it up a bit to what I think you might have meant:

SELECT ....
  case when consumer_expert_id = 1 then 1 else -1 end as my
  from consumer_expert_score
 where consumer_expert_score_id IN (2,1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM