根据目前的最新订购日期和检索结果(生日列表)的SQLite [英] Ordering of Dates and retrieving results based on current date (Birth Day List) SQLite

查看:162
本文介绍了根据目前的最新订购日期和检索结果(生日列表)的SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作为字符串存储在一个SQLite列中的日期,格式为YYYY-MM-DD(不分成的一年日期和月份三列)现在我有一个要求,进行查询,让我结果与最接近的日期的日期查询被制造出来的第一个条目和顺序的推移在上升order.Here我没有兴趣在这一年中,只有月​​份和日子事项。按我的要求,我处理,这样我才能得到这些日期是什么类型的查询。

I have a date stored as string in a Sqlite column, the format is YYYY-MM-DD ( Not separated into three columns for year date and month) Now I have a requirement to make a query which gives me results with the closest date to the date the query is being made as the first entry and the order goes on in ascending order.Here I am not interested in the year, only the month and the day matters. As per my requirement, what type of query should I process so that I get these dates in order.

例如:

我有日期

1980年1月11号,1999年8月3日,2013年12月3日 我应该得到查询后:(基于当前日期,考虑到这是2013年12月1日)

1980-01-11, 1999-08-03, 2013-12-03 After the Query I should get:(Based on current date, considering it is December 1 2013)

2013-12-03,1999-01-11,1980-08-03(排序仅基于月,日)。

2013-12-03,1999-01-11,1980-08-03 (Sort based only on month and day).

我也参考了从这里开始:<一href="http://stackoverflow.com/questions/7343807/mysql-query-to-sort-upcoming-birthdays-based-on-current-date">MySQL查询到根据当前的日期即将到来的生日进行排序,但它是没有太大的清晰。我觉得Unix的时间戳存储在日期列将帮助查询我感兴趣的,而不是简单的日期格式类型,只要值。

I also took reference from here :MySQL query to sort upcoming birthdays based on current date But it is not much clear. I think Unix timestamps as long values stored in date column would aid the type of query I am interested in, rather than the simple date format.

推荐答案

下面会做什么:

SELECT *
FROM _table
ORDER BY SUBSTR(DATE('NOW'), 6)>SUBSTR(birthdate, 6), SUBSTR(birthdate, 6)

检查它在 SQL小提琴

SUBSTR(日,6)将删除部分每年从日期。出生日期必须在 MM-DD 进行排序,但首先应该显示那些今天之后会发生,然后将这些前已发生的今天。

SUBSTR(date, 6) will remove year part from date. Birthdates must be sorted in MM-DD, but first should be shown those which will happen after today, and then those which happen before today.

SUBSTR(DATE('现在'),6)&GT; SUBSTR(出生日期,6)将返回0生日 MM-DD 大于或等于今天 MM-DD ,否则为1。因此,第一八佰伴pression将订单过去的日期正在添加日期。第二个参数只是订单日期由 MM-DD

SUBSTR(DATE('NOW'), 6)>SUBSTR(birthdate, 6) will return 0 for birthdates MM-DD greater or equal to today MM-DD, 1 otherwise. So, first expression will ORDER past dates after comming date. Second parameter just ORDER dates by MM-DD.

走一步看一步的 SQL小提琴

这篇关于根据目前的最新订购日期和检索结果(生日列表)的SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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