duangu6431 2015-08-23 00:05
浏览 21
已采纳

MYSQL按三列排序

I currently have a MYSQL table set out as: Date Started, last post date, sticky thread

I'm currently doing

ORDER BY sticky DESC, lastpostdate DESC, date DESC

Which almost works, it puts the sticky thread at the top (as it should be), however it is then putting the newest thread at the bottom and threads with replies at the top even if the new thread was created after the reply.

Example table:

 Date         lastpostdate   sticky
 1440291308 | 0            | 0
 1938275440 | 1938293999   | 1
 1440205782 | 1440212200   | 0

It should be ordered 2,1,3 because 2 is a sticky, 1 is a new thread and 3 has a reply but was before 1 was posted.

  • 写回答

1条回答 默认 最新

  • duannaxin9975 2015-08-23 00:25
    关注

    This is working as expected since you have a lastpostdate set to 0. The subsequent sort columns will ONLY be used if the values are equal on the previous sort column.

    For example, if both the sticky values are set to 0, then the order looks to the next sort column. The third column is only consulted if the first and second sort columns are equivalent.

    You should have the lastpostdate set to the date when the thread is initially created and this would solve your problem and rid you of the need to have the third sort column.

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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用