Q:

Mysql sort by base_price or discount_value if not null

I want to sort data by price. but I have 2 column data tables namely base_price and discount_value. I want to sort it out by comparing it if there is a discount value and the small value will be sorted. but in this problem I can only sort according to discount_value or base_price does not combine both. is there a way to overcome it? 
Query :
select `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, pc.id as product_color_id, `pc`.`base_color`, `i`.`image`, sum(ps.stock) as stock, `p`.`status` from `products` as `p` 
left join `category_product` as `cp` on `p`.`id` = `cp`.`product_id` 
left join `categories` as `c` on `cp`.`category_id` = `c`.`id` 
left join `product_colors` as `pc` on `p`.`id` = `pc`.`product_id` 
left join `image_product_color` as `ipc` on `ipc`.`id` = (select ipc1.id from image_product_color as ipc1 where pc.id = ipc1.product_color_id order by ipc1.id desc limit 1) 
left join `images` as `i` on `i`.`id` = (select i1.id from images as i1 where ipc.image_id = i1.id order by i1.id desc limit 1) 
left join `product_sizes` as `ps` on `pc`.`id` = `ps`.`product_color_id` 
left join `product_discounts` as `pd` on `pd`.`id` = (select pd1.id from product_discounts as pd1 where p.id = pd1.product_id and date(now()) <= pd1.valid_until) 
group by `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, `pc`.`id`, `pc`.`base_color`, `i`.`image`, `p`.`status` 
order by CASE WHEN pd.discount_value = null THEN p.base_price ELSE pd.discount_value END ASC

Result :

A:

Change your ORDER BY clause to the following:
ORDER BY IFNULL(pd.discount_value, p.base_price) ASC

This is how your revised query looks like:
select `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, pc.id as product_color_id, `pc`.`base_color`, `i`.`image`, sum(ps.stock) as stock, `p`.`status` from `products` as `p` 
left join `category_product` as `cp` on `p`.`id` = `cp`.`product_id` 
left join `categories` as `c` on `cp`.`category_id` = `c`.`id` 
left join `product_colors` as `pc` on `p`.`id` = `pc`.`product_id` 
left join `image_product_color` as `ipc` on `ipc`.`id` = (select ipc1.id from image_product_color as ipc1 where pc.id = ipc1.product_color_id order by ipc1.id desc limit 1) 
left join `images` as `i` on `i`.`id` = (select i1.id from images as i1 where ipc.image_id = i1.id order by i1.id desc limit 1) 
left join `product_sizes` as `ps` on `pc`.`id` = `ps`.`product_color_id` 
left join `product_discounts` as `pd` on `pd`.`id` = (select pd1.id from product_discounts as pd1 where p.id = pd1.product_id and date(now()) <= pd1.valid_until) 
group by `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, `pc`.`id`, `pc`.`base_color`, `i`.`image`, `p`.`status` 
ORDER BY IFNULL(pd.discount_value, p.base_price) ASC;

