简体   繁体   English

如何订购和选择条件查询

[英]How to order and select query with conditions

I have the following query: 我有以下查询:

SELECT * FROM table WHERE some conditions ORDER price ASC

The whole query is a bit more: 整个查询要多一些:

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;  }}

what I would like to do is the same query but is the price is 0.00 this need to be the last instead of the first. 我想做的是相同的查询,但是价格是0.00,这需要是最后一个而不是第一个。

For example I have the following products: 例如,我有以下产品:

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

I would like to order them as follows: 我想按以下顺序订购它们:

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

However the order I get is: 但是我得到的顺序是:

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

Any help welcome 任何帮助欢迎

Instead of: 代替:

ORDER BY price ASC

use: 采用:

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

Demo here 在这里演示

Assuming you want to order by price ascending except when price is 0 then it needs to be last. 假设您要按价格升序进行订购,除非价格为0,那么它必须是最后一个。

ORDER BY (CASE WHEN price = 0 THEN 1 ELSE 0 END) ASC, price ASC

If your price contains the actual pound sign, first of all that isn't good database design, but second of all, you'll have to modify the query to: 如果您的价格包含实际的英镑符号,那么首先,这不是很好的数据库设计,但首先,您必须将查询修改为:

ORDER BY (CASE WHEN price = '£0'  THEN 1 ELSE 0 END) ASC, price ASC

This way the first priority ordering happens by a pseudo field that is only 1 when the price is 0 and will be ordered ascending, this means any price != 0 will be first before and price == 0. After that, we continue to order by price ascending. 这样,当价格为0且将被升序排列时,只有一个为1的伪字段发生第一优先级排序,这意味着任何价格!= 0将在价格之前先被定价,而price ==0。此后,我们继续订购价格上升。 in your example, this would be: 在您的示例中,这将是:

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

Keep in mind, this will not work for any negative prices. 请记住,这对于任何负价格都将无效。 If negative prices to also show up last, you'll have to modify the CASE statement to apply to price <= 0 instead. 如果负价格也最后出现,则必须修改CASE语句以应用于price <= 0

Try: ORDER BY Price = 0, Price ASC 试试: ORDER BY Price = 0, Price ASC

The Price = 0 condition in the ORDER BY clause will return a value of 0 or 1 if the result is false or true respectively. 如果结果分别为falsetrue ,则ORDER BY子句中的Price = 0条件将返回值01 And by default it will be sorted in ascending order. 默认情况下,它将按升序排序。

Therefore, the prices which are equal to 0 will have a result of 1 (true) base from the Price = 0 condition, and will be placed on the last rows because 1 (true) is greater than 0 (false) . 因此,等于0Price = 0Price = 0条件下的结果为1 (true) ,并且由于1 (true)大于0 (false)而将其放在最后一行。

See the SQL Fiddle sample 请参阅SQL Fiddle示例

Even if the data type of Price is DECIMAL , equating it to 0 or 0.00 will sort on the order that you want. 即使Price的数据类型为DECIMAL ,将其等于00.00也会按所需顺序排序。

Take a look at this another SQL Fiddle sample. 看看这另一个SQL Fiddle示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM