Friday, March 14, 2008

Getting bit by MySQL Dates

Yesterday morning, Jack sent out a rather perplexing problem to Josh and myself.

Basically, he reported, this query doesn't return any results, despite there being plenty of valid records:

select * from injections where DATE(created_at) = DATE('2008-03-11');

And this query returns the expected records:

select * from injections where DATE(created_at) = DATE('2008-02-18');

The only difference between the two queries is the date provided. Crazy.

But wait, it's gets worse!

This query yields 293 rows (distinct days):

select distinct DATE(created_at) from injections;

While this one only returns 277 rows (days):

select distinct DATE(created_at) from injections where DATE(created_at) = DATE(created_at);

Josh provided some interesting links to MySQL documentation with tidbits like:
"MySQL automatically converts a date to a number if the date is used in a numeric context"
and
"However, this conversion is subject to the following exceptions: ... For these exceptional cases, the comparison is done by converting the objects to strings and performing a string comparison."
and this one, which I suspect is the culprit:
"A microseconds part is allowable in temporal values in some contexts, such as in literal values, and in the arguments to or return values from some temporal functions."
I emphasized the use of the word "some" in that quote. When an API is sometimes consistent - an oxymoron if I've ever heard one - it should set off the klaxons in your noggin.

We didn't solve the puzzle, but Jack did find a clever resolution:

...it turns out that "created_at LIKE '2008-03-11%'" is about 10% faster.

40 comments:

AmberlyThrower said...

台灣情kiss色網a片下載xxx383美女寫真go2av免費影片18jack主入口視訊美女mybankxx369色女人專用米克綜合論壇85cc免費聊天室台中酒店S援交彩虹馬子免費影片視訊情人後宮電影主入口視訊交友av1688視訊甜心寶貝av貼片區a片免費試看avdvd85cc免費影片視訊聊天bt成人網85cc免費影片線上觀賞百分百貼影片區成人視訊一葉晴貼片免費影片線上直播

瘋狂的 said...

Man proposes, God disposes...................................................

太可怕 said...

女優大奶性交性愛淫蕩淫慾淫亂淫婦淫妹淫叫淫水淫女情慾情色做愛限制級波霸口交18禁貼圖寫真視訊援交露點爆乳潮吹裸體裸照裸女愛愛無碼尋夢視訊聊天a漫

彥宏 said...

thank for share, it is very important . ̄︿ ̄..................................................

怡均怡均怡均 said...

I do like ur article~!!!...................................................

楊怡均 said...

海鷗要高飛,必先遠退。花蜜要香醇,必先久釀。......................................................

IdellaD_Bar3321 said...

長見識了,謝謝你! ........................................

Ko212ryS_Dahle0 said...

心中有愛,才會人見人愛。 ..................................................

RafaelHartin1 said...

sex女,ut,貼影,av,高潮,女優,做愛,手機成人影片,色遊戲,成人動漫,百分百貼圖區,85cc影片,成人影音,av色情影片,A片線上,a片,sex,777,三級線上看,美女的照片,視訊做愛,78論壇,打飛機,免費視訊,成人影院,辣妹視訊,視訊聊天,影片網,kiss911,a片,交友,聊天,做愛,免費影片,性交,線上成人,網路色情,聊天,美女自慰,免費a分享,免費短片,裸照,一夜情,女優,85cc成人片,美女寫真,偷拍a,情人視訊聊天室

吳wyat031tskiles8 said...

Nice post ~ 3Q..............................................................

家信 said...

keep update, please..bless you!!.........................

K910athrinA_Petrin0 said...

欣賞是一種美德~回應是最大的支持^^.............................................

麗卿 said...

死亡是悲哀的,但活得不快樂更悲哀。 ..................................................

柏懿綺辰 said...

優質的好部落格,需要大家的支持! ........................................

黃峻帆 said...

亞洲成人文成人dvd影城線上免費看成人卡通成人色情電影院色情卡通貼圖新中部聊天室新免費成人影片新性愛影片暗黑破壞神程式暗黑程式豦美鳳豦美鳳光碟圖片台灣論壇圖片區85論壇彰化人留言板彰化人聊天事彰化交友聊天室彰化聊天是熊熊貼圖區熊貓[情色貼圖熊貓av貼圖熊貓人貼圖熊貓人圖片熊貓人影片熊貓天堂圖片85論壇嘟嘟線上影片觀賞嘟嘟網線上免費a片豦美鳳性愛光碟嘟ㄉ成人網視訊聊天室 免費視訊 聊天免費視訊

瓊文TamMcfee0520 said...

世間是非,要如水泥地般水過則乾。..................................................

許美玉 said...

一沙一世界,一花一天堂,掌中握無限,剎那即永恆......................................................................

峻龍 said...

要持續更新下去喲!!祝你心情愉快.................................................................

李哲維 said...

要照顧身體歐~保重......................................................................

石白 said...

一棵樹除非在春天開了花,否則難望在秋天結果。....................................................................

于庭 said...

成熟,就是有能力適應生活中的模糊。.................................................................

懿綺懿綺 said...

走過路過~不能錯過~哈哈............................................................

林琇 said...

Thank you very much for such useful informaion ● 3●..................................................................

芸茂芸茂 said...

憤怒,是片刻的瘋狂。..................................................

宜欣宜欣 said...

蛛絲馬跡皆學問、落花水面皆文章............................................................

楊儀卉 said...

Practice makes perfect.............................................................

楊儀卉 said...

真有內容的部落格~推推!.................................[/url]...............

銘木 said...

回應是最大的支持^^y~~~甘吧嗲..................................................................

盈廖生家秀蔡 said...

天氣涼了~要注意身體喔@~@............................................................

RicoLisi0802志竹 said...

大肚能容,了卻人間多少事,滿腔歡喜,笑開天下古今愁。..................................................

劉隆季劉隆季 said...

Learning makes life sweet.

姿弘綸穎 said...

工作,是愛的具體化~~~~努力吧!........................................

家唐銘 said...

Necessity is the mother of invention..................................................................

偉曹琬 said...

做好事,不需要給人知道,雖然只是一件微不足道的事,但我相信,這會帶給我快樂。..................................................

偉曹琬 said...

愛情不是慈善事業,不能隨便施捨。......................................................................

怡屏 said...

生命如夏花洵爛;死如秋葉之靜美。........................................ ........................

張王雅竹欣虹 said...

期待你的下次更新喔^____^..................................................

阿袁袁袁袁華 said...

很棒的分享~希望不會打擾您..................................................................

怡靜怡靜怡靜怡雯 said...

變天了~~注意身體,別感冒囉!............................................................

junjun said...

人生最重要的一件事,就是從生活中認識你自己。................. ................................................