Order by alias in mysql contained in if query

2019-08-06 00:08发布

问题:

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



标签: mysql alias