Mysql按基本价格或折扣值排序(如果不为空)
我想按价格对数据进行排序。但我有两列数据表,即基本价格和折扣值。我想通过比较它来排序,如果有折扣值,那么小的值将被排序。但在这个问题中,我只能根据折扣值或基本价格进行排序,而不能将两者结合起来。有办法克服它吗 查询: 结果:Mysql按基本价格或折扣值排序(如果不为空),mysql,database,sorting,Mysql,Database,Sorting,我想按价格对数据进行排序。但我有两列数据表,即基本价格和折扣值。我想通过比较它来排序,如果有折扣值,那么小的值将被排序。但在这个问题中,我只能根据折扣值或基本价格进行排序,而不能将两者结合起来。有办法克服它吗 查询: 结果: 将订单条款更改为以下内容: ORDER BY IFNULL(pd.discount_value, p.base_price) ASC 以下是修改后的查询的外观: select `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`b
将订单条款更改为以下内容:
ORDER BY IFNULL(pd.discount_value, p.base_price) ASC
以下是修改后的查询的外观:
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;
尝试按IFNULLpd订购。折扣价格,p.base价格作为其工作感谢broI已将其作为答案,请接受并投票:
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;