mysql query - show last month's data unix timestamp - mysql

I have made this query and it works correct to show data for a month from the current date. For example this query:
SELECT *
FROM test
WHERE price >=100
AND active = 1
AND dateadded >= UNIX_TIMESTAMP(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))
ORDER BY testvalue DESC
This query outputs date time stamp from i.e.30th October till the current date 30th November. I have edited the query and takeout CURDATE but it fails at the point.
The question is how would I change this query to just show only last months i.e. October's data if the month is November.

You have to get the range between the first day of the previous month, which you can get by formatting the date and setting the day part to 01 and the last day of the previous month. There's a function for that. On the same page in the manual you also find information about DATE_FORMAT().
SELECT *
FROM test
WHERE price >=100
AND active = 1
AND dateadded BETWEEN
UNIX_TIMESTAMP(DATE_FORMAT(DATE_SUB( CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01'))
AND UNIX_TIMESTAMP(CONCAT(LAST_DAY(DATE_SUB( CURDATE(), INTERVAL 1 MONTH), ' 23:59:59'))
ORDER BY testvalue DESC

Related

Select next 7 days using only day and month

I have a table with birthdays in them, formatted in YYYY-MM-DD. My aim is to return results if the birthday is within the next 7 days.
I need it to use only the month and day, because if it reads the year also, a birthday in 1993 is never going to be within the next 7 days. It also needs take month changes into consideration.
For example, if its the 28th of Feb, and a birthday in the table is on the 1st March, that would be within 7 days, but not within the same month.
SELECT * FROM user WHERE DATE_FORMAT(birthday, '%m-%d') >= DATE_FORMAT(NOW() - INTERVAL 7 DAY, '%m-%d')
This is where i'm at, at the moment, but I know i'm miles off.
You need to format both of your dates in the WHERE clause.
SELECT *
FROM user
WHERE DATE_FORMAT(birthday, '%m-%d') BETWEEN DATE_FORMAT(NOW(), '%m-%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 7 DAY), '%m-%d')
retrieve current year with YEAR(CURDATE(), make a date biy concatenating with month and day coming from birthday and remove 7 days, check if curdate is in the range :
select *
from user
where
Curdate() between date_sub(CONCAT(YEAR(CURDATE()),'-', date_format(birthday, '%m-%d')), interval 7 day)
and CONCAT(YEAR(CURDATE()),'-', date_format(birthday, '%m-%d')) ;
Take year part from today's date and concatenate it with the month and day part of the dob column value. And cast that string to date. Then use that as a sub-query and check whether thet new column day difference is between 0 and 7.
Query
SELECT t.* -- select except the `new_col`
FROM (SELECT *,
CAST((Concat(YEAR(NOW()), '-', DATE_FORMAT(dob, '%m-%d'))) AS DATE
) AS
`new_col`
FROM `user`)t
WHERE DATEDIFF(t.`new_col`, NOW()) BETWEEN 0 AND 7;

sql query to show records created today, last week and last month

I want to write some sql queries to show me records from mysql database that are created today, records created last week, and records created last month..The first query show me results created in last 7 days (including today). For example if today is Sunday, I want to see results created from Monday to Saturday (not today). Similarly, in the second query I want to see records created last month (excluding records of this month). For example, if this is June, I want to see records created in May
SELECT COUNT(*) AS stdtotal FROM `login`
WHERE `account_created_date` > DATE_SUB(NOW(), INTERVAL 7 DAY)
and the other query is
SELECT COUNT(*) AS stdtotal FROM `login`
WHERE `account_created_date` > DATE_SUB(NOW(), INTERVAL 30 DAY)
Note that my table name is login and the column name is account_created_date of type date.
Just fix the where clauses. For the previous 7 days:
WHERE account_created_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND
account_created_date < CURDATE()
For the previous month:
WHERE account_created_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE) - 1 DAY), INTERVAL 1 MONTH) AND
account_created_date < DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE) - 1 DAY)
This calculates the first day of the current month and then goes one month prior to that.

How to decrease SQL Query from last 30 days until now?

I am trying to get the amount of data for the last 30 days.
SELECT ( Now() - interval 1 month ),
Count(flightid) AS count
FROM flight
WHERE flightstatus = 0
AND flightvisibility = 1
AND flightvaliddate > Now()
AND flightvaliddate >= ( Now() - interval 1 month )
Right now this is working ok and it's giving me only 1 row that corresponds to the same day of last month.
What I would like is to get the remaining data from each day until now. How can I do this?
I am using MySQL.
The condition in the WHERE clause is wrong.
And since you want day wise data of last thirty days till now then you must have to use GROUP BY.
SELECT
DATE(flightvalidate) AS flightValidateDate,
Count(flightid) AS count
FROM
flight
WHERE
flightstatus = 0
AND flightvisibility = 1
AND DATE(flightvaliddate) >= CURDATE() - INTERVAL 1 MONTH
GROUP BY flightValidateDate
ORDER BY flightvalidate

Query that displays rows in a range of date with day, month and year

I have the following columns in my table Log:
year, month, day, info
I need a query that selects the rows in a range of date determined by the user. The user will select the initial day, month and year and also the final day, month and year. At the moment, my query is:
SELECT
CONCAT(LPAD(`day`,2, 0),'/',LPAD(`month`,2, 0),'/',`year`) AS data,
info
FROM
Log
WHERE
(year > :initial_year OR (year = :initial_year AND month >= :initial_moth AND day >= :initial_day))
AND (year < :final_year OR (year = :final_year AND month <= :final_month AND day <= :final_day))
GROUP BY
year, month, day
ORDER BY
year DESC, month DESC, day DESC
But this query doesn't display any results, even that they are in the database! What is wrong and how can I fix it?
Your logic is wrong:
WHERE (
year > :initial_year OR (
year = :initial_year AND month >= :initial_moth AND day >= :initial_day
)
)
Will exclude any dates in your initial year where the day portion is greater than the initial day portion. e.g. yyyy-01-31 as the initial day will exclude all results for yyyy where the day portion is not 31.
Similar problems exist with the final date.
As suggested in the comments, use one DATE field in your database and do the other fiddling in your application code; it will save a lot of drama.
If you can't change the database, find and berate the person who designed it until they change it. If you can't do that then:
WHERE (year>:initial_year OR (year=:initial_year AND (month>:initial_month OR (month=:initial_month AND day>=:initial_day))))
and similar for the final date
What about:
select
concat(year,month,day) as thedate, info
from
log
where
thedate >= :startdate and thedate <= :enddate
order by
thedate desc;

Selecting all rows from last month onward

I'm trying to select all the data from the previous month to all the months in the future .. for example , I'd like to select everything from January till any date available in the future on the database, that goes for any month .. select the previous month till the future months of this year
This is my QUERY, It only starts with February , how can I make it start with the previous month .. current month - 1 is not working
SELECT *
FROM events
WHERE YEAR(event_start_date) = YEAR(CURDATE())
AND MONTH(event_start_date) = MONTH(CURDATE())
use DATE_SUB() to select previous month and >= to select all data in the future:
SELECT *
FROM events
WHERE YEAR(event_start_date) = YEAR(CURDATE())
AND MONTH(event_start_date) >= MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))
Try:
AND MONTH(event_start_date) = MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTHS))
The portion DATE_SUB(CURDATE(), INTERVAL 1 MONTH) will subtract a month from the current date. If you want last month and everything in the future, use:
AND MONTH(event_start_date) >= MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTHS))
Notice '>='. Although there is an edge case at january that you'll have to get around. The best way might be like this:
WHERE event_start_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 MONTHS), (DAYOFMONTH(CURDATE)) DAYS)
Which will get you everything from the 1st of last month. No edge cases.
Try this
SELECT *
FROM events
WHERE YEAR(event_start_date) = YEAR(CURDATE())
AND MONTH(event_start_date) >= MONTH(CURDATE() - INTERVAL 1 MONTH);
SELECT *
FROM events
WHERE event_start_date >= '1/'+MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))+'/'+YEAR(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))
For performance, you'd likely want an index range scan operation on the event_start_date column. That means (obviously), you'd want an index with event_start_date as a leading column.
To get an index range scan, the predicate needs to be on the bare event_start_date column, and NOT a function.
WHERE event_start_date >= some_value
For "some_value" in this case, one possible expression you can use would be:
CAST(DATE_FORMAT(NOW()+INTERVAL -1 MONTH ,'%Y-%m-01') AS DATE)
That takes the current date and time, subtracts one month, and then sets the day and time component to midnight of the first of the month.