views:

140

answers:

4

hi guys, My table is using a datetime (YYYY-MM-DD HH:MM:SS) and i need to display today's entries.

my code is only :

SELECT   * 
FROM     table 
WHERE    date = '$date' 
ORDER    BY score DESC

with

$date = date("Y-m-d");

well, as expected it doesnt work :| you guys have a solution here ?

+1  A: 

Your date is "2009-12-19" (or something like that, depending on the day), which is interpreted as "2009-12-19 00:00:00".

In your database, you probably don't have any date that's exactly equal to that one, by the second : your dates are like "2009-12-19 12:15:32".

A solution is to compare like this :

select *
from table
where date >= '2009-12-19'
    and date < '2009-12-20'

Which will be interpreted as :

select *
from table
where date >= '2009-12-19 00:00:00'
    and date < '2009-12-20  00:00:00'

And, if you don't want to do the math to get the date of the following date, you can use the adddate function :

select *
from table
where date >= '2009-12-19'
    and date < adddate('2009-12-19', interval 1 day)

So, in your case, something like this should do the trick :

select *
from table
where date >= '$date'
    and date < adddate('$date', interval 1 day)
order by score desc
Pascal MARTIN
isn't there a more beautiful option to write that ?thx for the tips anyway, that would have took some thinking
David 天宇 Wong
Hu ; we don't agree on the definition of "beautiful", I suppose -- at least, I don't think this looks "ugly", but, well... ;; an advantage of this solution is that it should use the index you have on the "date" column, if you have any on that one (and maybe you should) -- while a solution that converts the "date" column or does a calculation on it will remove the abolity to use an index.
Pascal MARTIN
what do you mean by index ?and by beautiful, I find the solution of Daniel Vassallo more clean
David 天宇 Wong
Heu... 600 characters won't be enough to explain what an index is, actually ^^ But you can take a look at this page of the manual, as a start : http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
Pascal MARTIN
A: 

You probably want to format the data when you select it:

SELECT *, DATE_FORMAT(date, '%Y-%m-%d') AS dateformat FROM table
WHERE dateformat = '$date' ORDER BY score DESC
DisgruntledGoat
A: 

You are comparing datetime and date expression, that is why its not working. Use Date() method to return the date part from datetime and then do the comparison. WHERE DATE(date) = '$date' should do. You might have to use aliases to handle this name collision.

amrinder
+1  A: 

Following from Pascal Martin, you could extract the date part from the date+time field:

SELECT * FROM table WHERE DATE(date) = '2009-12-19'

Source: MySQL - Date and Time Functions

Be aware however, that this query will not use an index on your date+time field, if you will be having one. (Stack Overflow: How does one create an index on the date part of DATETIME field in MySql)

Daniel Vassallo
that's the nicest way to do it =) thx a lot
David 天宇 Wong
If I'm not mistaken, with a solution like this one, in which there is a calculation on the "date" column, that calculation/conversion will have to be made on every line of the table -- ruining the chance of using the index that might have been set on the "date" column.
Pascal MARTIN
@Pascal: Yes, I think so. I've added the clarification in my answer... In the meantime, I am checking how MySQL handles this, for curiosity's sake.
Daniel Vassallo
@Daniel : OK ;; thanks for the precision :-) And I'm curious about it too, just to know if what I think is actually true or not ^^
Pascal MARTIN
@Pascal: Looks like you are right: http://stackoverflow.com/questions/95183/. Your solution is the way to go when you require the use of an index.
Daniel Vassallo
@Daniel : thanks for the search, the update, and the link :-)
Pascal MARTIN