datetime - mysql help trying to compare time -



datetime - mysql help trying to compare time -

i'm prepping add together new column ('closed_date') mysql database calculated based on datetime field ('scrape').

specifically, if time of field scrape (which datetime) > 6pm set closed_date date of scrape time of 23:59:00. else set closed_date scrape interval -1 days time of 23:59:00.

i'm stuck getting time comparing work.

here's fiddle schema...

drop table if exists mytable; create table mytable ( scrape datetime, average_rating int, number_of_ratings int, factored_rating int); insert mytable select '2014-05-29 00:13:55',5,2,3 union select '2014-05-29 00:14:42',4,1,5 union select '2014-05-29 21:08:18',12,3,1 union select '2014-05-30 00:14:46',11,4,2 union select '2014-05-30 21:08:22',8,2,12;

here's sql...

select scrape, date_format(scrape,'%h:%i:%s') timeonly, date_format(str_to_date('18:00:00','%t'),'%t') sixpm, @x := if(@timeonly > @sixpm, date_format(scrape, '%y-%m-%d 23:59:00'), date_add(scrape, interval -1 day)) close_date mytable

the result:

scrape timeonly sixpm close_date may, 29 2014 00:13:55+0000 00:13:55 18:00:00 2014-05-28 00:13:55 may, 29 2014 00:14:42+0000 00:14:42 18:00:00 2014-05-28 00:14:42 may, 29 2014 21:08:18+0000 21:08:18 18:00:00 2014-05-28 21:08:18 may, 30 2014 00:14:46+0000 00:14:46 18:00:00 2014-05-29 00:14:46 may, 30 2014 21:08:22+0000 21:08:22 18:00:00 2014-05-29 21:08:22

the if false when timeonly greater sixpm.

thanks insight!

i figured out. timeonly field wasn't available if comparison, had create temp field (@tim)...

select scrape, @tim := date_format(scrape,'%h:%i:%s'), @six := date_format(str_to_date('18:00:00','%t'),'%t'), if(@tim > @six, date_format(scrape, '%y-%m-%d 23:59:00'), date_format(date_add(scrape, interval -1 day),'%y-%m-%d 23:59:00')) close_date mytable

now result expect:

scrape close_date may, 29 2014 00:13:55+0000 may, 28 2014 23:59:00 may, 29 2014 00:14:42+0000 may, 28 2014 23:59:00 may, 29 2014 21:08:18+0000 may, 29 2014 23:59:00 may, 30 2014 00:14:46+0000 may, 29 2014 23:59:00 may, 30 2014 21:08:22+0000 may, 30 2014 23:59:00

p.s. ended doing field update via python:

for row in cur: print row stkid = row[0] # check if after 5pm close... if int(row[1].strftime("%h")) > 17: cldt = row[1].strftime("%y-%m-%d") else: # else it's in wee hours, need subtract day cldt = (row[1] - relativedelta(days=1)).strftime("%y-%m-%d") print "greater 5 pm...", row[1].strftime("%h"), row, cldt

mysql datetime time

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -