I have a table with two columns like so:
- price (varchar)
- discount (varchar)
These two hold the price and discount for all of the products in my database. When users view the products, I have multiple sorting modes available, one of which is Sort by price: low to high. In order to do this, I have the following code:
$stmt = $link->prepare("SELECT ... ORDER BY `discount` ASC, `price` ASC, `title` ASC");
This works fine for all rows where discount is defined, but in rows where discount is empty, they are sorted above the others, regardless of the value of price.
For example:
id|price|discount
-----------------
1|20 |10
2|25 |10
3|15 |
4|15 |
Will echo out in the order:
3, 4, 1, 2
How can I rewrite this statement to sort by price when discount has no value?
price - discount, then shouldn't youORDER BY price - discount ASC?ORDER BY price-ifnull(discount,0)is more appropriate