gpt4 book ai didi

mysql - 如何排序和选择带条件的查询

转载 作者:行者123 更新时间:2023-11-29 01:35:25 25 4
gpt4 key购买 nike

我有以下查询:

SELECT * FROM table WHERE some conditions ORDER price ASC

整个查询有点多:

SELECT * FROM products WHERE some conditions ";
if (($_GET['Category']) =='X'){
switch( $_GET['price'] ){
case '':
$query_RS_Search .= ' some conditions ORDER BY price ASC';
break;
case '0-500':
$query_RS_Search .= ' some conditions ORDER BY price ASC';
break;
case '500-1000':
etc
break;

}else{switch( $_GET['price'] ){
case '':
$query_RS_Search .= ' some conditions ORDER BY price ASC';
break;
case '0-500':
$query_RS_Search .= ' some conditions ORDER BY price ASC';
break;
case '500-1000':
etc
break; }}

我想做的是相同的查询,但价格是 0.00,这需要是最后一个而不是第一个。

例如我有以下产品:

product 1 price £15
product 2 price £25
product 3 price £12
product 4 price £0

我想按如下方式订购它们:

product 3 £12
product 1 £15
product 2 £25
product 4 £0

但是我得到的顺序是:

product 4 £0
product 3 £12
product 1 £15
product 2 £25

欢迎任何帮助

最佳答案

代替:

ORDER BY price ASC

使用:

ORDER BY IF(price = 0, 1, 0), price ASC

Demo here

关于mysql - 如何排序和选择带条件的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49551214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com