按日期和日期排序时间降序排列? [英] Sorting by date & time in descending order?

查看:615
本文介绍了按日期和日期排序时间降序排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部 我想显示最后5个输入的特定ID的数据. 我的SQL查询是

all I want to display last 5 entered data for specific id. My sql query is,

SELECT id, name, form_id, DATE(updated_at) as date
  FROM wp_frm_items
  WHERE user_id = 11 && form_id=9
  ORDER BY updated_at DESC

updated_at是DATETIME

updated_at is DATETIME

它显示按日期而不是时间排序的最后5个条目.然后在同一日期按字母顺序排序.

It displays last 5 entry sort by date not by time. On same date then it is sorting alphabetically.

假设我在同一日期有3个条目,且带有差异时间

Suppose i have 3 entries in same date with diff time

Ajay 1/3/2012 1:15
John 1/3/2012 1:00
Bony 1/3/2012 1:10

查询上述查询后

我得到的是

Ajay 1/3/2012 1:15
Bony 1/3/2012 1:10
John 1/3/2012 1:00

按日期排序,然后按字母顺序排序

Sort by date then after alphabetically

我想要的是这个..

John 1/3/2012 1:00
Bony 1/3/2012 1:10
Ajay 1/3/2012 1:15

还按日期和时间排序...

Sorted by date and time also...

推荐答案

如果您想要最后5行(以升序排列),则需要一个子查询:

If you want the last 5 rows, ordered in ascending order, you need a subquery:

SELECT *
FROM
    ( SELECT id, name, form_id, DATE(updated_at) AS updated_date, updated_at
      FROM wp_frm_items
      WHERE user_id = 11 
        AND form_id=9
      ORDER BY updated_at DESC
      LIMIT 5
    ) AS tmp
ORDER BY updated_at


第10次阅读问题后,这可能是(可能只是)您想要的.按日期顺序降序,然后按时间(同一日期)升序:


After reading the question for 10th time, this may be (just maybe) what you want. Order by Date descending and then order by time (on same date) ascending:

SELECT id, name, form_id, DATE(updated_at) AS updated_date
FROM wp_frm_items
WHERE user_id = 11 
  AND form_id=9
ORDER BY DATE(updated_at) DESC
       , updated_at ASC

这篇关于按日期和日期排序时间降序排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆