订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
mysql

mysql日期函数

mysql日期函数,很有用的,值得收下。。。

mysql> SELECT something FROM table
WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30;

DAYOFWEEK(date)
返回日期date的星期索引(1=星期天,2=星期一, ……7=星期六)。这些索引值对应于ODBC标准。
mysql> select DAYOFWEEK('1998-02-03');
-> 3

WEEKDAY(date)
返回date的星期索引(0=星期一,1=星期二, ……6= 星期天)。
mysql> select WEEKDAY('1997-10-04 22:23:00');
-> 5
mysql> select WEEKDAY('1997-11-05');
-> 2

DAYOFMONTH(date)
返回date的月份中日期,在1到31范围内。
mysql> select DAYOFMONTH('1998-02-03');
-> 3

DAYOFYEAR(date)
返回date在一年中的日数, 在1到366范围内。
mysql> select DAYOFYEAR('1998-02-03');
-> 34

MONTH(date)
返回date的月份,范围1到12。
mysql> select MONTH('1998-02-03');
-> 2

DAYNAME(date)
返回date的星期名字。
mysql> select DAYNAME("1998-02-05");
-> 'Thursday'

MONTHNAME(date)
返回date的月份名字。
mysql> select MONTHNAME("1998-02-05");
-> 'February'

QUARTER(date)
返回date一年中的季度,范围1到4。
mysql> select QUARTER('98-04-01');
-> 2

WEEK(date)
 
WEEK(date,first)
对于星期天是一周的第一天的地方,有一个单个参数,返回date的周数,范围在0到52。2个参数形式WEEK()允许
你指定星期是否开始于星期天或星期一。如果第二个参数是0,星期从星期天开始,如果第二个参数是1,
从星期一开始。
mysql> select WEEK('1998-02-20');
-> 7
mysql> select WEEK('1998-02-20',0);
-> 7
mysql> select WEEK('1998-02-20',1);
-> 8

YEAR(date)
返回date的年份,范围在1000到9999。
mysql> select YEAR('98-02-03');
-> 1998

HOUR(time)
返回time的小时,范围是0到23。
mysql> select HOUR('10:05:03');
-> 10

MINUTE(time)
返回time的分钟,范围是0到59。
mysql> select MINUTE('98-02-03 10:05:03');
-> 5

SECOND(time)
回来time的秒数,范围是0到59。
mysql> select SECOND('10:05:03');
-> 3

PERIOD_ADD(P,N)
增加N个月到阶段P(以格式YYMM或YYYYMM)。以格式YYYYMM返回值。注意阶段参数P不是日期值。
mysql> select PERIOD_ADD(9801,2);
-> 199803

PERIOD_DIFF(P1,P2)
返回在时期P1和P2之间月数,P1和P2应该以格式YYMM或YYYYMM。注意,时期参数P1和P2不是日期值。
mysql> select PERIOD_DIFF(9802,199703);
-> 11

DATE_ADD(date,INTERVAL expr type)
 
DATE_SUB(date,INTERVAL expr type)
 
ADDDATE(date,INTERVAL expr type)
 
SUBDATE(date,INTERVAL expr type)
这些功能执行日期运算。对于MySQL 3.22,他们是新的。ADDDATE()和SUBDATE()是DATE_ADD()和DATE_SUB()的同义词。
在MySQL 3.23中,你可以使用+和-而不是DATE_ADD()和DATE_SUB()。(见例子)date是一个指定开始日期的
DATETIME或DATE值,expr是指定加到开始日期或从开始日期减去的间隔值一个表达式,expr是一个字符串;它可以以
一个“-”开始表示负间隔。type是一个关键词,指明表达式应该如何被解释。EXTRACT(type FROM date)函数从日期
中返回“type”间隔。下表显示了type和expr参数怎样被关联: type值 含义 期望的expr格式
SECOND 秒 SECONDS
MINUTE 分钟 MINUTES
HOUR 时间 HOURS
DAY 天 DAYS
MONTH 月 MONTHS
YEAR 年 YEARS
MINUTE_SECOND 分钟和秒 "MINUTES:SECONDS"
HOUR_MINUTE 小时和分钟 "HOURS:MINUTES"
DAY_HOUR 天和小时 "DAYS HOURS"
YEAR_MONTH 年和月 "YEARS-MONTHS"
HOUR_SECOND 小时, 分钟, "HOURS:MINUTES:SECONDS"
DAY_MINUTE 天, 小时, 分钟 "DAYS HOURS:MINUTES"
DAY_SECOND 天, 小时, 分钟, 秒 "DAYS HOURS:MINUTES:SECONDS"

MySQL在expr格式中允许任何标点分隔符。表示显示的是建议的分隔符。如果date参数是一个DATE值并且你的计算仅仅
包含YEAR、MONTH和DAY部分(即,没有时间部分),结果是一个DATE值。否则结果是一个DATETIME值。

mysql> SELECT "1997-12-31 23:59:59" + INTERVAL 1 SECOND;
-> 1998-01-01 00:00:00
mysql> SELECT INTERVAL 1 DAY + "1997-12-31";
-> 1998-01-01
mysql> SELECT "1998-01-01" - INTERVAL 1 SECOND;
-> 1997-12-31 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",
INTERVAL 1 SECOND);
-> 1998-01-01 00:00:00
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",
INTERVAL 1 DAY);
-> 1998-01-01 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",
INTERVAL "1:1" MINUTE_SECOND);
-> 1998-01-01 00:01:00
mysql> SELECT DATE_SUB("1998-01-01 00:00:00",
INTERVAL "1 1:1:1" DAY_SECOND);
-> 1997-12-30 22:58:59
mysql> SELECT DATE_ADD("1998-01-01 00:00:00",
INTERVAL "-1 10" DAY_HOUR);
-> 1997-12-30 14:00:00
mysql> SELECT DATE_SUB("1998-01-02", INTERVAL 31 DAY);
-> 1997-12-02
mysql> SELECT EXTRACT(YEAR FROM "1999-07-02");
-> 1999
mysql> SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03");
-> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03");
-> 20102

如果你指定太短的间隔值(不包括type关键词期望的间隔部分),MySQL假设你省掉了间隔值的最左面部分。例如,
如果你指定一个type是DAY_SECOND,值expr被希望有天、小时、分钟和秒部分。如果你象"1:10"这样指定值,
MySQL假设日子和小时部分是丢失的并且值代表分钟和秒。换句话说,"1:10" DAY_SECOND以它等价于"1:10" MINUTE_SECOND
的方式解释,这对那MySQL解释TIME值表示经过的时间而非作为一天的时间的方式有二义性。如果你使用确实不正确的日期,
结果是NULL。如果你增加MONTH、YEAR_MONTH或YEAR并且结果日期大于新月份的最大值天数,日子在新月用最大的天调整。

mysql> select DATE_ADD('1998-01-30', Interval 1 month);
-> 1998-02-28

注意,从前面的例子中词INTERVAL和type关键词不是区分大小写的。
TO_DAYS(date)
给出一个日期date,返回一个天数(从0年的天数)。
mysql> select TO_DAYS(950501);
-> 728779
mysql> select TO_DAYS('1997-10-07');
-> 729669

TO_DAYS()不打算用于使用格列高里历(1582)出现前的值。

FROM_DAYS(N)
给出一个天数N,返回一个DATE值。
mysql> select FROM_DAYS(729669);
-> '1997-10-07'

TO_DAYS()不打算用于使用格列高里历(1582)出现前的值。

DATE_FORMAT(date,format)
根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。

所有的其他字符不做解释被复制到结果中。

mysql> select DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');
-> 'Saturday October 1997'
mysql> select DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');
-> '22:23:00'
mysql> select DATE_FORMAT('1997-10-04 22:23:00',
'%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> select DATE_FORMAT('1997-10-04 22:23:00',
'%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
MySQL3.23中,在格式修饰符字符前需要%。在MySQL更早的版本中,%是可选的。

TIME_FORMAT(time,format)
这象上面的DATE_FORMAT()函数一样使用,但是format字符串只能包含处理小时、分钟和秒的那些格式修饰符。
其他修饰符产生一个NULL值或0。
CURDATE()
 
CURRENT_DATE
以'YYYY-MM-DD'或YYYYMMDD格式返回今天日期值,取决于函数是在一个字符串还是数字上下文被使用。
mysql> select CURDATE();
-> '1997-12-15'
mysql> select CURDATE() + 0;
-> 19971215

CURTIME()
 
CURRENT_TIME
以'HH:MM:SS'或HHMMSS格式返回当前时间值,取决于函数是在一个字符串还是在数字的上下文被使用。
mysql> select CURTIME();
-> '23:50:26'
mysql> select CURTIME() + 0;
-> 235026

NOW()
 
SYSDATE()
 
CURRENT_TIMESTAMP
以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前的日期和时间,取决于函数是在一个字符串还是在数字的
上下文被使用。
mysql> select NOW();
-> '1997-12-15 23:50:26'
mysql> select NOW() + 0;
-> 19971215235026

UNIX_TIMESTAMP()
 
UNIX_TIMESTAMP(date)
如果没有参数调用,返回一个Unix时间戳记(从'1970-01-01 00:00:00'GMT开始的秒数)。如果UNIX_TIMESTAMP()用一
个date参数被调用,它返回从'1970-01-01 00:00:00' GMT开始的秒数值。date可以是一个DATE字符串、一个DATETIME
字符串、一个TIMESTAMP或以YYMMDD或YYYYMMDD格式的本地时间的一个数字。
mysql> select UNIX_TIMESTAMP();
-> 882226357
mysql> select UNIX_TIMESTAMP('1997-10-04 22:23:00');
-> 875996580

当UNIX_TIMESTAMP被用于一个TIMESTAMP列,函数将直接接受值,没有隐含的“string-to-unix-timestamp”变换。

FROM_UNIXTIME(unix_timestamp)
以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回unix_timestamp参数所表示的值,取决于函数是在一个字符串
还是或数字上下文中被使用。
mysql> select FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00'
mysql> select FROM_UNIXTIME(875996580) + 0;
-> 19971004222300

FROM_UNIXTIME(unix_timestamp,format)
返回表示 Unix 时间标记的一个字符串,根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条
目同样的修饰符。
mysql> select FROM_UNIXTIME(UNIX_TIMESTAMP(),
'%Y %D %M %h:%i:%s %x');
-> '1997 23rd December 03:43:30 x'

SEC_TO_TIME(seconds)
返回seconds参数,变换成小时、分钟和秒,值以'HH:MM:SS'或HHMMSS格式化,取决于函数是在一个字符串还是在数字
上下文中被使用。
mysql> select SEC_TO_TIME(2378);
-> '00:39:38'
mysql> select SEC_TO_TIME(2378) + 0;
-> 3938

TIME_TO_SEC(time)
返回time参数,转换成秒。
mysql> select TIME_TO_SEC('22:23:00');
-> 80580
mysql> select TIME_TO_SEC('00:39:38');
-> 2378

平均得分
(0 次评分)





文章来自: http://www.zhenhua.org
标签: mysql 日期函数 
评论: 10 | 查看次数: 733
  • 共有 10 条评论
游客 [2008-12-08 22:25:36]
游客 [2008-11-19 15:23:19]
游客 [2008-11-11 09:10:01]
游客 [2008-11-06 11:32:39]
8GB MP3 PLAYER
Beyond knowing your limitations. apple ipod it's important to be able to size up opponents . Bluetooth Headset at a glance. Bluetooth Headsets Knowing their class is the first step, understanding. canon digital camera your places in a rock-paper-scissors scenario. cell phone accessories More important, however, is the estimation . digital camcorder of gear. digital camcorders. As much as skill and class balance . digital camera plays a part in the outcome . digital cameras of combat, gear is a major differentiator that makes up for shortcomings in other areas. dvd player In fact, with . dvd players the introduction of Resilience, gear more than . ipod shuffle ever plays a more substantial part in PvP. ipod touch In an Arena match, the very first thing we . mp3 scope out is gear. mp3 mp4 player Through quick tab-selection viewing . mp3 mp4 player of character portraits, we generally . mp3 mp4 player have a good idea of the classes we're up against if they keep their helm graphic on. mp3 player accessories If they are wearing Season 3 shoulders, then we know . mp3 player accessory their relative experience. mp3 player kaufen This is why the visual i. mp3 player mpact of Arena shoulders is so important. mp4 It immediately gives you a general idea of how tough the match will be. portable dvd player Players in full S3 will likely have over 10k hp and over. portable dvd players 400 Resilience, depending on the class and spec. zubehoer mp3 player A full S3 SL/SL Warlock, for example, will easily have. about 12-13k hp and over 400 Resilience. Identifying weapons is slightly more . difficult but will also give a general idea of an enemy's strength. Season 1 and 2 weapons share the same graphics, so it's harder to identify. Season 3 weapons, on the other 最新免费网络游戏 hand, are distinctive and share models with Black Temple and Mount Hyjal weapons. which have relatively the same power. A review of Brutal Gladiator weapons will come in handy because these will be the most common way to identify opponents of relative skill. With the new mechanics. in place for Arenas, Season 4 will more . or . less weed out the chaff from the grain.
游客 [2008-11-06 11:25:25]
1GB MP3 PLAYER A couple of people . 1gb mp3 players have posted about the Shattered Sun Peacekeepers slacking off on their jobs lately. best mp3 player quite possibly thanks . buy mp3 player to something Blizzard fed them . buy mp3 players in PatchIn their reports, they complain about Peacekeepers. cheap mp3 player attacking them for no reason, sometimes not even in retaliation for attacking (or defending yourself against). cheap mp3 players a member of the opposing faction. eve isk I can actually empathize with this as. gold wow I encountered the ill-placed wrath of the Peacekeepers myself when I rezzed my wife's toon in front of . hdro gold the Staging Area. lotro gold Without having done anything other. lotro gold than rezz, the Peacekeepers promptly charged . mp3 player and made short work of me. mp3 players
In my experience, I have found that the Peacekeepers around . mp4 player the Shattered Sun Staging Area have . mp4 players been slacking off. portable mp3 players In fact, my wife's toon was ganked right in front . wow of the building and the so-called Peacekeepers . wow gold did absolutely nothing. wow gold Sensing a bug, my wife . wow gold wrote a ticket and got a somewhat rude e-mail response saying that -- you guessed it -- it was working as intended. wow gold An Alliance . wow leveling guild on my server seemed to be aware of the fact and exploited it to full effect, killing solo players who could seek no refuge under the apathetic -- or even hostile -- Peacekeepers. wow oro According to reports, it's a known bug -- one player even . wow powerleveling made a video to document it -- but 网络游戏 so far Blizzard . has turned a blind eye to it. I got a more sympathetic response from my GM who at least. mentioned he'd look into the situation. An in-game GM even reset the Peacekeepers 网游in order to see if it would change anything (it didn't). I don't mind gankage.



1GB MP3 PLAYER My wife is a shrewd little fox. 2GB MP3 PLAYER She knows just how much I love the fact. 2GB MP3 PLAYER that she plays the game with me so sometimes, when we have our little domestic arguments, she makes sure. 4GB MP3 PLAYER to cancel her WoW account just to drive home a point. 4GB MP3 PLAYER Of course, it doesn't mean much since we're both paid up for the next few months, but the message is clear -- "we make up (or you see things my way). baladeur MP3 or I'm quitting the game!" Of course, we don't reconcile merely because I'll be losing my . baladeurs MP3 favorite playing partner, but I have to confess that it doesn't make . cell phones me happy one bit. cheap cell phones For parents, World of Warcraft can be a . digital mp3 player useful bargaining chip for their kids with the parental . ipod controls feature. ipod nano It's easy enough to control WoW time if kids aren't doing their homework, floundering in school, or simply not. ipods doing their chores. lecteur mp3 Conversely, a friend . lecteurs mp3 of mine gave his son a WoW subscription when . mp3 mp4 he did well in school. mp3 mp4 player World of Warcraft can be so. mp3 mp4 player much fun and addicting that it's . mp3 player often used as a social tool, and it's often upsetting when our. mp3 player kaufen friends quit playing the game. mp3 player wholesale How many of us have . mp3 players had friends whose significant others have "allowed" them to. mp4 player play the game after, say, a wonderful date. phones cell I'm not sure if it only applies to me, but . portable mp3 player because I play the game with many of my RL friends . and my family, I use the 免费网络游戏 lure of WoW . to full effect. I once had my brother do a specific task for . the promise of an upgrade to The Burning Crusade. A little before he finished what I asked him to do, I secretly upgraded his account. so he could finally make his Blood Elf Priest. Kind of manipulative, I know, but we did end up having . a lot of fun leveling our alts together. How about you. How much 最新网游 a part of your life is WoW and has it . ever been used as a bargaining . chip in your social life.
游客 [2008-11-06 11:19:50]
buy wow gold Remember the WoW account hacking. buying gold world of warcraft If you're a WoW player. cheap wow gold you couldn't have missed . cheap wow gold any of the articles posting this issue. cheap wow gold Well, as it turns out, an article posted by . cheapest wow gold grimwell says that someone might have found the problem behind all the . eve isk account hacking, stealing and selling even 5 year's worth items. free online games A piece of the BBC news article says:"Analysis [. free online war games mp3 mp3 player ] showed that. mp3 players it lay dormant on a victims . mp4 machine until . online games they ran World of Warcraft (WoW) at which point it captured login data and sent it to. play war games the hacking group. sell wow gold The group's enthusiastic use of the cursor flaw suggests it is trying to . world of warcraft gold do the same again. wow The online fantasy game . wow gold now has more than eight million active players around the world. wow gold Research by security firm Symantec . wow gold suggests that the raw value of a WoW account is now higher. wow gold than a credit card and its associated verification data. wow gold "Normally, as a WoW player, you'd know about the risks involved魔兽 when creating an online account, yet some were . wow gold lazy enough not to check on their accounts every once in a while, thus resulting in their items . wow gold kaufen and WoW currency being stolen. wow gold kaufen Everyone knows that . stuff like that happen, some. even managed to hack the 游戏 Superbowl website and use it . to host code for spyware, so monitor the hell out of your computer!In an article I wrote yesterday about hackers seeing console users as the new target, there is a comment coming from Stefana Muller.
游客 [2008-10-29 11:50:40]
游客 [2008-10-22 10:14:10]
游客 [2008-10-22 10:08:35]
1、70后:工作狂基本上都是70后的。
80后:而我们,拒绝加班!
90后:拒绝上班!
world of warcraft accountsworld of warcraft account
2、 70后:他们喜欢穿七匹狼或者猛龙牌子的衣服。
  80后:我们喜欢G-Star之类的。
  90后:乞丐服,越花越好,越破越好..一个洞时尚,两个洞潮流,三个洞个性...
wow accountswow account
3、 70后:他们唱k的时候只会乱吼──例如2002年的第一场雪,然后就拼命拉着你喝酒,不让你唱。
  80后:Mic霸一般是我们。
  90后:我们不止会唱,还会跳!
sell accountssell account
4、 70后:他们的话题除了工作就是股票。
  80后:我们的话题更多,有英超、魔兽……
  90后:QQ等级,QQ秀...
buy accountsbuy account
5、 70后:他们如果有笔记本,会喜欢到公众场合用。
  80后:我们才不会背那么重的东西在身上。
  90后:只要苹果笔记本,而且不止一台...
Arena rating Power levelingArena points Power leveling
6、 70后:他们喜欢喝红酒,一般是长城红酒。
  80后:我们要么不喝酒,要么就喝啤酒。
  90后:韩国果汁,日本汽水...
wow Arena ratingwow Arena points
7、 70后:他们无论任何时候,看到有站着的领导,都会马上给领导让座。
  80后:我们崇尚上下级平等。
  90后:天上地下,唯我独尊!
Warhammer OnlineWarhammer Online Power leveling
8、 70后:他们娶老婆的时候想娶处女。
  80后:我们觉得无所谓,只要相互感情好就可以了。
  90后:结婚需要感情吗?..需要结婚吗?..
Warhammer Online PowerlevelingWarhammer Powerleveling
9、 70后:他们觉得每个日本人、美国人、台湾人都想攻打中国。
  80后:我们喜欢日本的连续剧、台湾的综艺节目、美国的大片。
  90后:我要去日本,因为我是日系MM...
Warhammer Online Power levelingWarhammer Powerleveling
10、70后:他们希望中国用核弹把上面三个国家(地区)都灭了。
  80后:我们希望和平。
  90后:和我无关!打仗衣服会降价吗?那就打呗~~
War power levelingWar powerleveling
11、70后:他们对服务员态度恶劣,或者言语上调戏女服务员。
  80后:我们只在点菜和结帐时会跟服务员说话。
  90后:从不和waitress说话,只会背后讨论她的衣服很土...
War powerleveling
12、70后:他们有存款。
  80后:我们负债。
  90后:我们有老爸!
游客 [2008-10-20 09:53:56]
  • 共有 10 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启