Awan
Awan

Reputation: 18600

Query records based on datetime column

I have a column of type datetime in MySql table. Stored values are like this:

2008-02-15 17:21:56
2008-02-15 17:22:02
2008-02-15 17:22:03

Now I want to query some records based on this column but I have only date part not time part. So I am querying like this in zend.

$select->where('tableName.columnName = ?', '2008-02-15' );

But it does not match any record. How can I query without time part.

Thanks

Upvotes: 0

Views: 259

Answers (2)

Jarod Elliott
Jarod Elliott

Reputation: 15670

The answer @Shota has provided will work, however it will prevent MySQL from using any indexes on the datetime column due to the DATE function having to be called on every row of the table to see if it matches.

If you hard code the time range into the query it will give the same result and still allow indexes to be used.

eg. $select->where('tableName.columnName >= ? AND tableName.columnName <= ?', '2008-02-15 00:00:00', '2008-02-15 23:59:59' );

Please note i may have the php syntax wrong but you should get the idea.

Upvotes: 2

Headshota
Headshota

Reputation: 21449

try this WHERE DATE(tableName.columnName) = '2008-02-15'

Upvotes: 3

Related Questions