简体   繁体   English

在WHERE子句中使用SELECT IF

[英]Using SELECT IF in WHERE clause

In the query below the SELECT statement has an IF case, if the price in table a is equal to 999999 than I set n_price to 0 otherwise the price stays the same. 在下面的查询中,SELECT语句具有IF情况,如果表a中的价格等于999999而不是我将n_price设置为0,否则价格保持不变。

What I want to know is would there be a simple way of using n_price (the updated value from the query) in the WHERE clause instead of a.price to carry out a calculation? 我想知道的是,有一种简单的方法可以在WHERE子句中使用n_price(查询中更新的值)而不是a.price来执行计算吗? if not what would be a simple way to retrieve the value in the WHERE clause 如果不是,那么在WHERE子句中检索值的简单方法是什么

SELECT 
IF(a.price = 999999, 0, a.price)            AS n_price, 
l.quantity                                  AS n_quantity,
l.condition                                 AS n_condition

FROM b_products b  

LEFT JOIN a_products a ON b.id = a.id
LEFT JOIN l_products l ON b.id = l.id

WHERE 
AND ROUND((a.price/100*80) - l.price) >= (l.price/100*30) 

I think this should work: 我认为这应该有效:

AND ROUND((IF(a.price = 999999, 0, a.price)/100*80) - l.price) >= (l.price/100*30) 

You can't use the name n_price you defined in the select clause. 您不能使用在select子句中定义的名称n_price

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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