I have a MySQL table called history that includes a column called month: January, February, March, etc. and a column called day_num containing day numbers from 1 to 31.
I need to be able to select a month and a day from the two corresponding columns, based on the current month and day.
I know it's a rather embarrasingly basic question, but - how do I do that?
SELECT month, day_num FROM history WHERE ??????? ORDER BY RAND() LIMIT 1;
Would appreciate a bit of advice from a knowledgeable person.
SELECT *
FROM History
WHERE DATE_FORMAT(CURDATE(), '%M') = `month` AND
DAY(CURDATE()) = `day_num`
SQLFiddle Demo
OR
SELECT *
FROM History
WHERE MONTHNAME(CURDATE()) = `month` AND
DAY(CURDATE()) = `day_num`
SQLFiddle Demo
Other Sources
MONTHNAME
DAY
Related
I am trying to get records from quarter before current quarter in MySQL. But not able to figure out how the query will be constructed.
My MySQL table:
Here's my query to get records of current quarter (Jan 2020 to March 2020):
SELECT * FROM `customers` WHERE QUARTER(`customer_date`) = QUARTER(CURRENT_DATE)
AND YEAR(`customer_date`) = YEAR(CURRENT_DATE)
ORDER BY `customer_date` DESC
But when I am using following query to get records from quarter before current quarter i.e. records from Oct 2019 to Dec 2019, the query doesn't yield me correct result. Actually it fetches records of every quarter (Oct to Dec) of all previous years in the table:
SELECT * FROM `customers` WHERE QUARTER(`customer_date`) = QUARTER(CURRENT_DATE - INTERVAL 1 QUARTER)
ORDER BY `customer_date` DESC
I can't use YEAR in above query. If I use the year then query will fail when it will be run in the future e.g. in the quarter of Apr 2020 to Jun 2020.
How can this query be modified to get required result?
Try this will do:
select
*
from
customers
where
quarter(`customer_date`) = quarter(current_date - interval 1 quarter)
and year(customer_date) =
(case
when quarter(curdate()) > 1 then year(current_date)
else year(current_date)-1
end)
order by
`customer_date` desc
db<>fiddle
I want to display last 12 months sales in a chart. SQL table has year and month field and not a combined date field.
Im not able to give the interval condition of 12months on Year field.
SELECT s_month,s_year,SUM(s_amount) FROM table
WHERE s_month >= Date_add(now(),interval - 12 month)
AND s_year >= Date_add(now(),interval - 12 month)
GROUP BY s_year,s_month
One method is:
select s_year, s_Month, sum(s_amount)
from t
where date(concat_ws('-', s_year, s.month, 1)) >= curdate() - interval 12 month
group by s_year, s_month;
You may want to adjust the date arithmetic, depending on whether you want the date from 12 months ago.
If you want the last 12 months in the data, you can do:
select s_year, s_month, sum(amount)
from t
group by s_year, s_month
order by s_year desc, s_month desc
limit 12;
This is a strong argument against storing date parts (month, year) in separate columns.
The WHERE clause you have does not do what you expect!
It is virtually always better to have a DATE column (or TIMESTAMP or DATETIME) and use date functions as needed to split it apart.
SELECT MONTH(dat), YEAR(dat), SUM(amount)
FROM table
WHERE dat >= CURDATE() - INTERVAL 12 MONTH
GROUP BY LEFT(dat, 7) -- eg, "2017-12"
There is another problem with your query. SUM(amount) will have a partial month at either end. I can't solve that for you without better understanding where the data comes from and when. If it is already a single reading stored on the first of the month, then no problem. If it is daily or hourly amounts, then my point stands.
I have a record set of sales amount daily with different branches.
For example
Date Amount Branch
01/01/2014 30 A
01/01/2014 30 B
01/02/2014 40 A
01/02/2014 40 B
01/03/2014 30 A
01/03/2014 30 B
up to feb,mar,apr,may,jun,jul,aug
What i want to achieve is to group the record monthly based on todays date day.
For example today is 08/11/2014. the range should be 1st day of the month "1" then i will pick the day today which is 11. So the range for all the months is 1-11. See below sample.
Date Range for query monthly
01/01/2014-01/11/2014
02/01/2014-02/11/2014
03/01/2014-03/11/2014
04/01/2014-04/11/2014
05/01/2014-05/11/2014
06/01/2014-06/11/2014
07/01/2014-07/11/2014
08/01/2014-08/11/2014
Group this date range and get the sum of total sales.
Please help
This should do most of the work:
SELECT MONTH(date), SUM(amount)
FROM table_name
WHERE DAY(date) <= DAY(CURDATE())
AND date >= YEAR(CURDATE())
GROUP BY MONTH(date);
UPDATE
For the 3 letter month tag, also you'll probably want an ORDER BY to be sure:
SELECT DATE_FORMAT(date,'%b'), SUM(amount)
FROM table_name
WHERE DAY(date) <= DAY(CURDATE())
AND date >= YEAR(CURDATE())
GROUP BY MONTH(date)
ORDER BY MONTH(date);
You should be able to achieve what you want by using the following MySQL query:
select sum('amount') from 'some_table'
where dayofmonth('sell_date') >= 1
and dayofmonth('sell_date') < dayofmonth(currdate())
group by month('sell_date');
I hope it works, did not have some database to test.
You could eventually also group by branch, by adding an additional , 'branch' before the query's semicolon.
I have the following relevant columns in my 'orders' table:
Date_Day (is a range from 1 to 31 with no trailing 0)
Date_Month (is a range from January to December, not numerical)
Date_Year (is the year in 4 digit format, ex: 2005)
Total (number with 2 decimal places)
I know the way of storing date is absolutely awful, but this was the database I was given. I am trying to find a few things and I'm not sure if there is a way to do it in SQL instead of doing the math in PHP:
The SUM of each day of each year.
The SUM of this day last year
(where this day is the nth weekday of the month. So for today, it
would be the 1st Tuesday of October in 2012)
The highest grossing
day in history
MySQL is not my forte, and while I can figure it out in PHP, I would love to see it done in MySQL so I can start to learn it more.
If you want to keep your database structured as it is, you could use these queries:
The SUM of each day of each year:
SELECT Date_Year, Date_Month, Date_Day, SUM(Total)
FROM tablename
GROUP BY Date_Year, Date_Month, Date_Day
The SUM of this day last year:
SELECT SUM(Total)
FROM tablename
WHERE
Date_Year = YEAR(CURDATE())-1
AND Date_Month = MONTHNAME(CURDATE())
AND Date_Day = DAY(CURDATE())
The highest grossing day in history:
SELECT Date_Year, Date_Month, Date_Day, SUM(Total)
FROM tablename
GROUP BY Date_Year, Date_Month, Date_Day
ORDER BY SUM(Total) DESC
LIMIT 1
my datefield (artists.onlineDate) is yyy-mm-dd hh:mm:ss
right now I got:
-- Today
SELECT * FROM artists WHERE DATE(artists.onlineDate) = CURDATE()
-- Current Week
SELECT * FROM artists WHERE WEEK(artists.onlineDate, 3) = WEEK(CURDATE(), 3)
-- Current Month
SELECT * FROM artists WHERE MONTH(artists.onlineDate) = MONTH(CURDATE())
-- Current Year
SELECT * FROM artists WHERE YEAR(artists.onlineDate) = YEAR(CURDATE())
But what I need is exact: Yesterday, Last Week, Last Month, Last Year
I try to explain. if we got wednesday, and I use SELECT * FROM artists WHERE DATE(artists.onlineDate) = CURDATE(), then I get monday to wednesday.
I would like to have the monday to sunday of the last week. the calendar week before.
And the same for the month and year.
I thins SUB_DATE is not the right way.
Any suggestions?
OK I found what I was looking for at:
MySQL Query to select data from last week?
SELECT * FROM testwoche
WHERE WEEK (datum, 3) = WEEK(current_date, 3) - 1
AND YEAR(datum) = YEAR(current_date) ORDER BY datum DESC
same for month
SELECT * FROM testwoche
WHERE month (datum) = month(current_date) - 1
AND YEAR(datum) = YEAR(current_date)
ORDER BY datum DESC
This gives back the last week from monday to sunday and the last month
Thanks everybody for helping!
I think you can use the SUBTIME function: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
I haven't had time to really test it, but I think you get the idea:
Yesterday:
SELECT * FROM artists WHERE DATE(artists.onlineDate) = SUBTIME(CURDATE(),'1 0:0:0');
Last Week
SELECT * FROM artists WHERE WEEK(artists.onlineDate, 3) = WEEK(SUBTIME(CURDATE(),'7 0:0:0'), 3)
Last Month
SELECT * FROM artists WHERE MONTH(artists.onlineDate) = MONTH(SUBTIME(CURDATE(),'31 0:0:0'))
Last Year
SELECT * FROM artists WHERE YEAR(artists.onlineDate) = YEAR(SUBTIME(CURDATE(),'365 0:0:0'))
To get a specific week/month/year (which still works when your db contains several years worth of data and when your current time is the start of a new year), this should work:
SELECT * FROM testwoche WHERE
((YEAR(CURDATE())-YEAR(datum)))*52-WEEK(datum,3)+WEEK(CURDATE(),3) = 1;
If you want two weeks ago, you could change it to =2 (if you want current week, from Monday to current day: =0). If you want last month, you change the WEEK function to MONTH.