how can i query my mysql database and fetch rows which are posted in earlier 3 days
i know how to fetch todays's rows but not 3 days ago
time will save in my table like this :
2010-01-20 19:17:49
and this is what i know :
SELECT id FROM pages WHERE date=now()
but i need to show posts in 3days
and im looking for a simple and straight solution ,because i know how to do so in long php codes
To get records three days in the past up to current time:
SELECT t.id
FROM PAGES t
WHERE t.date BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW()
This should select all entries with a date value after and including 3 days ago:
Select id From pages Where NOW() >= Interval date day + 3
Note that if there are dates in the future this will select them too.
Would be helpful to know what version of MySQL you're using and what column type we're talking about but here's the reference of Date and Time Functions for 5.1. You're going to probably want DATE_SUB().
Related
I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.
I want to create a query for fetch all the data from database. But condition is get from before current month and data will convert into 3 days slot wise with count.Like if i have 12 month data and in July 1 to 3 date data will insert into 50 rows and from 3 to 6 date data will insert into 5 rows.How i fetch this things.That is the mail problem. I have wondered but nothing found related my problem. Is this possible.
I assume you have a DATE or a DATETIME column, that marks the day of the insert, say the column created_at. Then you could get the desired result.
SELECT
COUNT(*)
FROM
your_table
WHERE
created_at BETWEEN '2014-07-01' AND '2014-07-31 23:59:59'
GROUP BY
(DAYOFMONTH(created_at) - 1) DIV 3
Explanation:
With the help of the DIV operator you get your 3-day-slots.
Note
If there's a slot without rows, it won't be in the result. To get these too, you could use a LEFT JOIN with the maximum 11 slots.
Demo
If you having timestamps as attribute type then might be this help you
SELECT count(*) as count
FROM table
AND
TIMESTAMPDIFF(DAY,date,2014-08-01)<=3";
The date is the attribute of your column, TIMESTAMPDIFF will find difference between given date if it's under 3 days from 2014-08-01 records will show up. This way by just sending one date into this position you can find 3 slots of date from your table.
I am trying to filter for rows that have a value of '1' in the active column and where the date in the lastfollowupemail_date column is 7 days or older from the current date.
Searching on the internet on many sites, this is the query I came up with, but for some reason it is not filtering correctly (the query is selecting records with a date less than 7 days from the current time).
Any ideas on why this may be occurring would be fantastic and much appreciated. Thank you so much in advance!
SELECT * FROM e_sales_prospects
WHERE active='1'
AND lastfollowupemail_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
if I'm reading this right you want rows that are older then seven days, in that case you want rows which have date smaller then so in my opinion it should be
lastfollowupemail_date < DATE_SUB(NOW(), INTERVAL 7 DAY)
I have an html table that displays maintenance records. There are 3 columns that have dates in the future.
I want to have a button above the table that checks whether any of these 3 dates are within the next 30 days. If so, the row is displayed and other rows that are not of immediate concern are not displayed.
What is the best approach for achieving a filter like this?
Update: I'm trying to do it in a MySQL query.
I have 3 attributes in the SQL table that are date.
Does anyone know how to query whether the dates are within the next 30 days?
Does anyone know how to query whether the dates are within the next 30 days?
Use this :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
uses between, curdate() and date_add()
Update
to check multiple dates you need to do :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND yournextdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND anotherdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
I am trying to write a query to select all records from users table where User_DateCreated (datetime field) is >= 3 months from today.
Any ideas?
SELECT *
FROM users
WHERE user_datecreated >= NOW() - INTERVAL 3 MONTH
If you want to ignore the time of day when a user was created you can use the following. So this will show someone created at 8:00am if you run Quassnoi's example query at 2:00pm.
SELECT *
FROM users
WHERE DATE(user_datecreated) >= DATE(NOW() - INTERVAL 3 MONTH)
Using DATE(user_datecreated) prevents mysql from using any indexes on the column, making the query really slow when the table grows.
You don't have to ignore the time when the user was created if you remove the time from the "3 months ago" date, as all the users created that day will match the condition.
SELECT *
FROM users
WHERE user_datecreated >= DATE(NOW() - INTERVAL 3 MONTH);