MySQL ORDER BY取决于CASE顺序ASC或DESC [英] MySQL ORDER BY depending on CASE order ASC or DESC
本文介绍了MySQL ORDER BY取决于CASE顺序ASC或DESC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要基于从另一个表顺序ASC或DESC查询的值.
I want based on a value queried from another table order ASC or DESC.
是这样的:
SELECT *
FROM table
ORDER BY
CASE (SELECT sorting from table2 WHERE table2.id = ?)
WHEN 1 THEN table.date ASC END
WHEN 0 THEN table.date DESC END
END
MySQL有类似的功能吗?
Is anything like that available in MySQL?
I've seen for MS-SQL Server some solution: how to order 2 SQL Fields in asc and desc dynamically
编辑:我只是看到描述中的错误,已解决.
EDIT: I just saw I made a mistake in the description, fixed.
推荐答案
order by if((select sorting from table2 where table2.id = ?) = 1,
unix_timestamp(table.date), -unix_timestamp(table.date))
如果列为数字,则
取反有效.如果是字符串,您也许可以找到另一个函数来将高值映射到低值...
negation works if your column is numeric. if it's a string, you might be able to find another function to map high values to low values...
这篇关于MySQL ORDER BY取决于CASE顺序ASC或DESC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文