Retrieve data of last month uploads - mysql

I'm trying to retrieve some data from a database using MySQL. I would like to SELECT just the records that I uploaded in the last month and not the previous ones. This script has to be dynamic : I mean that on 1st of February it should retrieve data from 1st of January to 1st of February and so on.
My table structure is really simple : the upload date is stored in the column 'reg_date' that is a TIMESTAMP type.
Does anyone know how to do this in a single query? Thanks!

This can all be done in a single SQL query using the CURDATE() and INTERVAL functions so no PHP date calculation is required.
SELECT *
FROM `TABLE`
WHERE `reg_date` BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()

You can filter the data like so:
select *
from your_table
where reg_date between date_sub(curdate(), interval 1 month) and curdate()

just use this query:
select * from your_table_name where reg_date between '2017-03-01' and '2017-03-31';

Related

Get records for last 2 days

I am trying to create a query to get the records for last two days. In my table there is a field called dates. Values are as below:
05-08-2018 08:05:22
05-08-2018 10:15:42
dd-mm-yyyy hh:ii:ss
I have created the query.
SELECT id,title,description, dates
FROM post_feed where `dates` BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
ORDER BY dates DESC LIMIT 100
When I run the query it return 0 records. It looks like issue with date format.
Seems like your dates are stored as varchar. You must convert them to date (e.g. by using STR_TO_DATE) before you can perform any comparison.
Assuming for example that today is Aug-05 and you want results for Aug-04 and 05 (inclusive):
SELECT id, title, description, dates
FROM post_feed
WHERE STR_TO_DATE(dates, '%d-%m-%Y') BETWEEN CURRENT_DATE - INTERVAL 1 DAY AND CURRENT_DATE
ORDER BY dates DESC
LIMIT 100
SQL Fiddle
Try this WHERE clause:
WHERE STR_TO_DATE(dates, '%d-%m-%Y %H:%i:%s') BETWEEN DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND CURDATE()
Your second date is before your first date. Put greater date at first place & put lesser date at second place.
SELECT id,title,description, dates
FROM post_feed where CAST(`dates`as date) BETWEEN CURDATE() AND CURDATE() - INTERVAL 1 DAY
ORDER BY dates DESC LIMIT 100
Fix your data! Your query doesn't work because the "date" value are stored as text/varchar. This is easily fixed:
update post_feed
set dates = str_to_date(date, '%Y-%m-%d %H:%i:%s')
alter table post_feed
modify column datetime;
Voila! Your queries will now work.

get all row entries based on month/year in MySQL

I have a database with the following structure and I am trying to get all rows from this table based on passing both the month and year using a where on the timestamp column (this will be a unix standard timestamp)
e.g month - 3, year - 2018 // get all rows for March 2018 only.
// db structure
id, timestamp, post_title
If you want rows for a given month using a unix timestamp, I would recommend:
where timestamp >= unix_timestamp('2018-03-01') and
timestamp < unix_timestamp('2018-04-01')
If you are passing in a variable, I would recommend passing in the first day of the month and doing:
where timestamp >= unix_timestamp(?) and
timestamp < unix_timestamp(? + interval 1 month)
Use convert function
SELECT * FROM dbo.YourTable WHERE CONVERT(VARCHAR(5),DATEPART(mm,timestamp))+'-'+CONVERT(VARCHAR(5),DATEPART(yy,timestamp)) ='3-2018'
I guess this can help you.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

SQL - Get result of current year only

How can I get the result of the current year using SQL?
I have a table that has a column date with the format yyyy-mm-dd.
Now, I want to do select query that only returns the current year result.
The pseudo code should be like:
select * from table where date is (current year dates)
The result should be as following:
id date
2 2015-01-01
3 2015-02-01
9 2015-01-01
6 2015-02-01
How can I do this?
Use YEAR() to get only the year of the dates you want to work with:
select * from table where YEAR(date) = YEAR(CURDATE())
Using WHERE YEAR(date) = YEAR(CURDATE()) is correct but it cannot use an index on column date if exists; if it doesn't exist it should.
A better solution is:
SELECT *
FROM tbl
WHERE `date` BETWEEN '2015-01-01' AND '2015-12-31'
The dates (first and last day of the year) need to be generated from the client code.
When I tried these answers on SQL server, I got an error saying curdate() was not a recognized function.
If you get the same error, using getdate() instead of curdate() should work!
--========= Get Current Year ===========
Select DATEPART(yyyy, GETDATE())
SELECT id, date FROM your_table WHERE YEAR( date ) = YEAR( CURDATE() )
SELECT
date
FROM
TABLE
WHERE
YEAR (date) = YEAR (CURDATE());
If the date field contains a time component, you want to include December 31 so you have to go to January 1 of the next year. You also don't have to use code to insert dates into the SQL. You can use the following
SELECT * FROM table
WHERE date BETWEEN MAKEDATE(YEAR(CURDATE()), 1) AND MAKEDATE(YEAR(CURDATE())+1, 1)
This will give you January 1st of the current year through January 1st at midnight of the following year.
As #Clockwork-Muse pointed out, if the date field does not contain a time component, you would want to exclude January 1 of the following year by using
WHERE date >= MAKEDATE(YEAR(CURDATE()), 1) AND date < MAKEDATE(YEAR(CURDATE())+1, 1)
You can do this using SQL DATE_FORMATE(). like below:
SELECT
date
FROM
TABLE
WHERE
DATE_FORMAT(date, '%Y') = YEAR (CURDATE());
SELECT [ID]
,[datefield]
FROM [targettable]
WHERE DATEPART(YYYY, [datefield]) = (SELECT TOP 1(MAX(DATEPART(YYYY, [datefield])))
FROM [targettable]
)
/*
This will find the newest records in the table regardless of how recent the last time data was entered.
To grab the oldest records from the table do this
SELECT [ID]
,[datefield]
FROM [targettable]
WHERE DATEPART(YYYY, [datefield]) = (SELECT TOP 1(MIN(DATEPART(YYYY, [datefield])))
FROM [targettable]
)
*/

sql telling how many days have passed(datediff)

I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;

Select mysql query between date?

How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'