dseax40600 2012-11-21 03:14
浏览 77
已采纳

在WHERE子句中使用SELECT IF

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.

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? if not what would be a simple way to retrieve the value in the WHERE clause

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) 
  • 写回答

1条回答 默认 最新

  • dongshun1884 2012-11-21 03:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路