Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL How to SELECT data from table which recorded today?

Use PHP and MySQL. In my table, there is date field (datetime) recorded by NOW() sql function. Example value of data in this field is 2010-10-07 10:57:36. How can I SELECT all data which day-month-year is today. I try to use code as below:

  SELECT * FROM table WHERE date=????
like image 841
AJ OP Avatar asked Oct 07 '11 05:10

AJ OP


People also ask

How do I query today's date in SQL?

To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34 .

How can I get current record in MySQL?

MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.

How do I select the latest record from a table in MySQL?

In SQL Server, we can easily select the last 10 records from a table by using the “SELECT TOP” statement. The TOP clause in SQL Server is used to control the number or percentage of rows from the result. And to select the records from the last, we have to arrange the rows in descending order.


2 Answers

Try this:

SELECT * FROM table WHERE date > CURDATE();

CURDATE() will return the current date as 2011-10-07 which will be cast to 2011-10-07 00:00:00 when comparing datetimes to it.

Note that if you use DATE(date) = CURDATE() you will run a date conversion for every row in the table, which will be really bad for your perfomance if you have many rows and/or you need to run the query often. Also make sure you have an index on date, otherwise both methods will be even slower.

like image 174
Kaivosukeltaja Avatar answered Oct 07 '22 19:10

Kaivosukeltaja


SELECT * FROM table where DATE(date)=CURDATE()

like image 30
Sergey Kudriavtsev Avatar answered Oct 07 '22 20:10

Sergey Kudriavtsev