Php mySQL语句按多列排序

Php mySQL语句按多列排序,php,mysql,sql,sql-order-by,Php,Mysql,Sql,Sql Order By,如果我有两列,价格(完整价格,销售价格),这些列中有数字。我知道sql语句可以执行多个order by,但是当这些列中有值时,它是如何工作的呢 ("SELECT * FROM table ORDER BY full_price,sales_price DESC") 我该如何实现这一点,使它能够从两列中选取最小的值?它将根据所选的完整价格和销售价格之间的列对数据进行排序 注:有时销售价格没有价值 谢谢 编辑: 范例 id full_price sales_price 1

如果我有两列,价格(完整价格,销售价格),这些列中有数字。我知道sql语句可以执行多个order by,但是当这些列中有值时,它是如何工作的呢

("SELECT * FROM table ORDER BY full_price,sales_price DESC")
我该如何实现这一点,使它能够从两列中选取最小的值?它将根据所选的完整价格和销售价格之间的列对数据进行排序

注:有时销售价格没有价值

谢谢

编辑:

范例

id    full_price        sales_price
1      23                 42           
2      342                200
3      1
4      10                 8
我想做的是用这些数字,我可以输出与最低价格相关的数据

命令应为:

3,4,1,2
原因:

3: 1
4: 8
1: 23
2: 200

如果您想要基于完整价格和销售价格差异的结果,请尝试:

SELECT *
FROM table
ORDER BY ABS(full_price - IF(sales_price IS NULL, 0, sales_price)) ASC
SELECT *
FROM table
ORDER BY IF(full_price < IF(sales_price IS NULL, 0, sales_price), 
            full_price , sales_price ) ASC
或者,如果您想要基于完整价格和销售价格比较的结果,请尝试:

SELECT *
FROM table
ORDER BY ABS(full_price - IF(sales_price IS NULL, 0, sales_price)) ASC
SELECT *
FROM table
ORDER BY IF(full_price < IF(sales_price IS NULL, 0, sales_price), 
            full_price , sales_price ) ASC
选择*
从桌子上
按IF订购(完整价格
如果您想要基于完整价格和销售价格差异的结果,请尝试:

SELECT *
FROM table
ORDER BY ABS(full_price - IF(sales_price IS NULL, 0, sales_price)) ASC
SELECT *
FROM table
ORDER BY IF(full_price < IF(sales_price IS NULL, 0, sales_price), 
            full_price , sales_price ) ASC
或者,如果您想要基于完整价格和销售价格比较的结果,请尝试:

SELECT *
FROM table
ORDER BY ABS(full_price - IF(sales_price IS NULL, 0, sales_price)) ASC
SELECT *
FROM table
ORDER BY IF(full_price < IF(sales_price IS NULL, 0, sales_price), 
            full_price , sales_price ) ASC
选择*
从桌子上
按IF订购(完整价格
假设您的空白
销售价格
为空,且
完整价格
不能为空:

select ...
from ...
where ...
order by case
    when sales_price is null then full_price
    else least(full_price, sales_price)
end

您可能需要添加一个辅助排序键,以便从领带中获得一致且合理的结果。

假设您的空白
销售价格
为空,并且
完整价格
不能为空:

select ...
from ...
where ...
order by case
    when sales_price is null then full_price
    else least(full_price, sales_price)
end

您可能希望添加一个辅助排序键,以从ties中获得一致且合理的结果。

我编辑以显示示例,谢谢我编辑以显示示例,谢谢我如何添加另一个排序键?我是将其添加到案例中还是在结尾之后?@andrewliu:您应该在
end
之后添加第二个排序键:
按案例排序。。。结束,其他列
如何添加另一个排序键?我是将其添加到案例中还是在结尾之后?@andrewliu:您应该在
end
之后添加第二个排序键:
按案例排序。。。结束,某个其他列