Order by alias in mysql contained in if query

2019-08-06 00:03发布

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;

标签: mysql alias
2条回答
该账号已被封号
2楼-- · 2019-08-06 00:15

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-08-06 00:27

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;
查看更多
登录 后发表回答