I have a table with some fields and a timestamp field named timestart.
What I would like to do is select all the records from my table where the field timestart is 21 days from now.
But how can I do this?
you can have this with. if you want exact equals to timestamp. use =
SELECT *
FROM table
WHERE date = DATE_ADD(NOW(), INTERVAL 21 DAY)
ORDER BY date DESC
you can achive the same by using
SELECT *
FROM table
WHERE date = DATE_ADD(NOW(), INTERVAL 21 DAY)
ORDER BY date DESC
The datediff function seems to meet the bill:
SELECT *
FROM my_table
WHERE DATE_DIFF (timestart, CURRENT_DATE()) >= 21
You can use this:
SELECT *
FROM table
WHERE date >= (NOW() - INTERVAL 21 DAY)
ORDER BY date DESC
LIMIT 20
Related
How can i display a range of date from my Date field in the database into a table?
I have a field called: Date from the database that Display the event date of 5 days. And I would like to display only 1 day and 12 hours (36 hours) using SQL Statement.
Field name : date, details from Events_new table. This table display 5 days of date and time records. and It's updates automatically. My point is that I need a SQL Query that will fetch the last 36 hours from the table
I tried many time. This is my SQl Statement but I can't get it right.
SELECT
details , datediff(date , interval 36 hour) as substratehours
FROM
events_new
WHERE
Stuff_id = 4932
order by date ASC
And This :
SELECT
date,
details
FROM
events_new
WHERE
stuff_id = 4932
AND date = (date - interval 36 hour)
And This:
SELECT
date,
details
FROM
events_new
WHERE
stuff_id = 4932
AND date > date_sub(date,interval 36 hour)
order by date ASC
I Still Don't get it right! Anyone with a subjection?
Thank you
To get the rows having date in the most recent 36 hours only:
SELECT details
FROM events_new
WHERE Stuff_id = 4932
AND `date` > date_sub(NOW(), INTERVAL 36 HOUR)
ORDER BY `date` ASC
If you don't want the 36 hours interval to end now but at some arbitrary moment in time then you should use BETWEEN to compare the value of field date with the interval bounds:
SELECT details
FROM events_new
WHERE Stuff_id = 4932
AND `date` BETWEEN date_sub('2015-01-19 12:00:00', INTERVAL 36 HOUR)
AND '2015-01-19 12:00:00'
ORDER BY `date` ASC
Replace '2015-01-19 12:00:00' with the moment when you want your 36 hours interval ends.
Take a look at MySQL documentation about the SELECT statement and date & time functions.
In mysql
SELECT DATE_ADD(date,INTERVAL -36 hour) AS date from events_new
In mssql
select DATEADD(hour , -36 , date )AS date from events_new
I have a table session_dates with some fields and a timestamp field named timestart.
What I would like to do is select all the records from my table where the field timestart (TIMESTAMP) is equal to 21 days from now.
Like for example if today is 27 januari -> 17 februari.
I know how I can select all between two dates.
My SQL Query for between 2 dates:
SELECT timestart, timefinish, sessionid
FROM sessions_dates
WHERE timestart BETWEEN UNIX_TIMESTAMP(NOW()) AND UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY))
But how to select equal to a date?
UPDATE:
I know now that I just have to use the = statement. But how can I test this? How do I know what the UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY)) returns?
I think you want:
SELECT timestart, timefinish, sessionid
FROM sessions_dates
WHERE timestart >= UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY)) AND
tmestamp < UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 22 DAY))
Presumably, timestart has a time component. This version takes that into account and still would allow the use of an index on timestart.
I'm trying to find a subset of users who joined a site within the last week
The table is users and date field (dateadded) is unix timestamp e.g. 2012-04-29 17:31:57
Here's what I'm trying but its returning all users:
SELECT * FROM users
WHERE dateadded <= NOW() AND dateadded >= DATE_SUB(dateadded, INTERVAL 7 DAY)
You have the wrong condition. You want now() for both comparisons:
SELECT *
FROM users
WHERE dateadded <= NOW() AND dateadded >= DATE_SUB(now(), INTERVAL 7 DAY)
I have this SQL:
$sql="SELECT *
FROM table
WHERE expiresdate >= Date(Now())
AND expiresdate <= Date_add(Date(Now()), INTERVAL 10 day)
ORDER BY expiresdate ASC";
it should basically show all rows in the database that are going to expire within 10 days time however, lets say the expiredate was 2013-03-06 - this row will not display on any day after the expiredate
does anyone have any ideas?
This should be what you need:
SELECT
*
FROM
`table`
WHERE
expiresdate <= CURDATE() + INTERVAL 10 DAY
ORDER BY
expiresdate ASC
I would like to rows that have only been entered in the last 1 day.
I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?
I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.
SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'
This query:
SELECT *
FROM mytable
WHERE mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
AND mydate < STR_TO_DATE('110321', '%y%m%d')
will return all records for Mar 20, 2011
From the MySQL manual (here):
SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;
SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
This returns all rows for today and yesterday.