dpxyfa4718 2013-04-18 16:29
浏览 53
已采纳

设置一个Mysql查询以按特定顺序显示数字列? [重复]

This question already has an answer here:

I have a user input box, querying a column of numbers in my SQL database, which, for example are:

2.0
3.0
4.0
5.0
6.0
7.0
8.0

The user will have to type in a number, and then populate a particular order of the list of numbers to respond to the input from the user. So if the user input was 4.0, then i want the database query to display

4.0,3.0,5.0,2.0,6.0,7.0,8.0.  

To break the results down, I want the result pattern to display the database results with the closest number to the input(4.0),then the next result, if available, to be lower by 1 degree(3.0), then higher by 1 degree(5.0),then lower by two degrees (2.0), and higher by two degrees(6.0). The 7.0 and 8.0 numbers are displayed numerically because there is no lows to match them.

How can I setup a query for this pattern of results?

</div>
  • 写回答

1条回答 默认 最新

  • dongyushen9063 2013-04-18 16:33
    关注
    SELECT  *
    FROM    mytable
    ORDER BY
            ABS(value - 4.0), SIGN(value - 4.0)
    

    See SQLFiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100