Getting MySQL data after specific date - mysql

How can I fetch MySQL data after a specific timestamp? What should the query look like?
mysql_query("SELECT * FROM table where TheNameOfTimestampColumn > than the date");

SELECT * FROM table WHERE TheNameOfTimestampColumn > '2009-01-28 21:00:00'
SELECT * FROM table WHERE TheNameOfTimestampColumn > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)

You can select this by :
select * from table_name where date_column > "2001-01-01 00:00:00"
or if you need data within certain time frame then you can try using between key word such as:
select * from table_name where date_column
between "2018-01-04 00:00:00" and "2018-01-04 11:59:59";
Note that date format should be in YYYY-MM-DD HH:MM:SS

If you're using a unix timestamp, you can do the following:
SELECT * FROM table WHERE TheNameOfTimestampColumn > FROM_UNIXTIME(your_time_stamp_here)

You can use MySQL DATE function like below
For instance, if you want results after 2017-09-05
SELECT DATE(timestamp_field) as date FROM stocks_annc WHERE DATE(timestamp_field) >= '2017-09-05'
Make sure to wrap the date within single quotation ''
Hope this helps.

Related

How to query with day and time field?

I have table with fields:
day (varchar)
timeFrom (time)
I have this record already
I need to find records that are less than tomorrow. But my query is giving empty result.
SELECT
*
FROM
table as b
WHERE
AND
(
b.`day` >= '2021-08-24' and b.time_from > '11:15:00'
)
You need to merge & cast your fields to build a datetime value, then you can use it in your where part or datetime functions:
SELECT *
FROM `table` AS b
WHERE cast(concat(b.`day`, ' ', b.time_from) AS datetime) > '2021-08-24 17:30:00'
This query should work.
you should add one day t now() function, then convert it to string and then compare it by you day filed.
Hope to help:
SELECT *
FROM yourtable
WHERE day < DATE_FORMAT(date_add(now(), interval 1 day)
In my opinion you cannot use a ">" operator between strings (varchars)
I think the solution will be to modify the table structure from varchar to datetime.

Comparing date with NOW() in MySQL

I have a column(expire_date(varchar)) with value stored like 2016-05-11 13:19:32. I want to compare this value with NOW(). I want to select the rows with the expiry_date greater than present time
My code:
select * from tbl_table where expiry_date > NOW()
But I don't know whether this is correct or not.
Try this:
select * from tbl_table where CAST(expiry_date AS DATETIME) > NOW()
If you want to compare DATETIME:
select * from tbl_table where str_to_date(expiry_date,"%Y-%m-%d %H:%i:%s") > NOW();
If you want to compare ONLY DATE:
select * from tbl_table where date(str_to_date(expiry_date,"%Y-%m-%d %H:%i:%s")) > curdate();
Note: Date should be stored as date or timestamp in database, instead of varchar DATE in MySQL.
Instead of now() use current date() like:
select * from tbl_table where expiry_date > current_date();

Filter by datetime MYSQL formatting

I have a Mysql Table that is used for a log file on the that table there is a field called 'log_date' And it stores the date in the following format( %Y-%m-%d %H:%i.%s ).On the DB the dates look like something this 2013-20-05 00:00.00. Lets say today's date is 2013-20-05 And I have log files from 2013-01-01 to present day. If I run a query like this:
SELECT * FROM log_table
WHERE STR_TO_DATE(log_date, '%Y-%m-%d %H:%i.%s') < '2013-05-05 00:00.00'
This is returning every row in the DB including rows that are greater than 2013-05-05 00:00.00
And if I reverse the < (less then) to a > (greater then) with a query that looks like this:
SELECT * FROM log_table
WHERE STR_TO_DATE(log_date, '%Y-%m-%d %H:%i.%s') > '2013-05-05 00:00.00'
Then it returns ZERO rows. I think the time stamp is what is causing the problem I have worked with the date format before but not the DateTime format. Why is this happening?
log_date should be of DateTime data type. It is much simpler to use MySQL DATE function. Some examples
SELECT * FROM log_table
WHERE DATE(log_date) < '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) > '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) BETWEEN '2013-04-05' AND '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) BETWEEN DATE(CURRENT_DATE() - INTERVAL 2 WEEK) AND
DATE(CURRENT_DATE() + INTERVAL 4 DAY)
You can try with that..
WHERE DATE_FORMAT(AUCTION_DATE, '%Y%m%d') >= DATE_FORMAT('2013/5/18', '%Y%m%d')
You can also get today date using now() function.

mysql: get record count between two date-time

I am stuck with a problem in MySQL. I want to get the count of records between two date-time entries.
For example:
I have a column in my table named 'created' having the datetime data type.
I want to count records which were created date-time between "TODAY'S 4:30 AM" and "CURRENT DATE TIME".
I tried some of MySQL's functions but still no luck with it.
Can you please help me with this?
thanks.
May be with:
SELECT count(*) FROM `table`
where
created_at>='2011-03-17 06:42:10' and created_at<='2011-03-17 07:42:50';
or use between:
SELECT count(*) FROM `table`
where
created_at between '2011-03-17 06:42:10' and '2011-03-17 07:42:50';
You can change the datetime as per your need. May be use curdate() or now() to get the desired dates.
select * from yourtable where created < now() and created > '2011-04-25 04:00:00'
select * from yourtable
where created < now()
and created > concat(curdate(),' 4:30:00 AM')
for speed you can do this
WHERE date(created_at) ='2019-10-21'

how to test date and datetime with mysql

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 ?
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)
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
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
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.