Order by alias in mysql contained in if query

一世执手 提交于 2020-01-05 12:58:52

问题


I am using a query to select price from price column without dollar and then order by that alias after if statement. But order by that alias name is not working.

Query which I am using is

SELECT 
  *, 
  IF( SUBSTRING( price , '1', '1' ) = '$', 
      round( replace( price , '$', '' ) ) ,
      price ) AS coupon 
FROM ccs_product 
WHERE (product_name LIKE '%JoyLot.com%' 
       OR website_name LIKE '%JoyLot.com%' 
       OR description LIKE '%JoyLot.com%')
ORDER BY coupon ASC 
LIMIT 0 , 10;

回答1:


use this query its works use round(price)

   SELECT 
       *, 
         IF( SUBSTRING( price , '1', '1' ) = '$', 
              round( replace( price , '$', '' ) ) ,
              round(price) ) AS coupon 
          FROM ccs_product 
         WHERE (product_name LIKE '%JoyLot.com%' 
     OR website_name LIKE '%JoyLot.com%' 
     OR description LIKE '%JoyLot.com%')
        ORDER BY coupon ASC 
   LIMIT 0 , 10;



回答2:


There is no problem using the alias to do the order by. Check this sql fiddle as proof



来源:https://stackoverflow.com/questions/14109076/order-by-alias-in-mysql-contained-in-if-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!