How to query data from MYSQLdb with date time now in Python 2.7?

I have got a table called ‘TEST’ and I have got a column called ‘UPLOAD_TIME’ what is a varchar(100) and the format is the next :

"2017-11-14 14:59:05.243974"

How can I query data from this table if i want to resemble my ‘UPLOAD_TIME’ column with the actual date now, and select data where year-month-day are equal with my data?

connection = MySQLdb.connect(host='localhost',
    user='admin',
    passwd='****',
    db='database1',
    use_unicode=True,
    charset="utf8")
cursor = connection.cursor()
query = """ select 'column1', 'column2', 'column3', 'column4', 'column5', 'column6', 'UPLOAD_TIME', 'ID'
    union all
    select `column1`, `column2`, `column3`, `column4`, `column5`, `column6`, `UPLOAD_TIME`, `ID`
    from TEST
    into outfile '/tmp/test.csv'
    fields terminated by ';'
    enclosed by '"'
    lines terminated by 'n';
    """
cursor.execute(query)
connection.commit()
cursor.close()
  • WHERE datetime =’2017-11-14′ is not an option for me, I want to resemble always to the actual date.

Answer

This should work.

WHERE DATE_FORMAT(UPLOAD_TIME,'%Y-%m-%d')='2017-11-14'

Link from comment: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate