计算字段中的MySQL IF条件 [英] MySQL IF Condition in a Calculated Field

查看:97
本文介绍了计算字段中的MySQL IF条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个计算字段,如果两个值恰好相同,则值会加倍。我在变量MY中编码这个条件。我是SQL的新手,所以我道歉。

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.

这是我的尝试:

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;

提前致谢!

推荐答案

您需要CASE WHEN和IN()子句。

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

不幸的是我无法完全重写你的查询,因为你的条件是重言式,它总会返回1.也许你的意思是别的?无论如何,我把它改成了一些我认为你的意思:

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)

这篇关于计算字段中的MySQL IF条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆