This question already has answers here:
MySQL Query GROUP BY day / month / year
(17 answers)
Closed 8 years ago.
I have a table called cc_calls and there I have many call records I want to count them and group them in months I have a timestamp called starttime and I can use that row to extract the month, also limit the count for 12 months
the results should be like:
Month Count
January 768768
February 876786
March 987979
April 765765
May 898797
June 876876
July 786575
August 765765
September 689787
October 765879
November 897989
December 876876
Can anyone guide me or show me the mysql query that I need to get this result.
SELECT
MONTH(starttime),
COUNT(*)
FROM cc_calls
GROUP BY MONTH(starttime)
Be aware though, that this counts for each month of every year. You might want to include the year in the select and group by clauses or filter for a specific year in the where clause.
SELECT
MONTH(starttime),
COUNT(*)
FROM cc_calls
GROUP BY (MONTH(FROM_UNIXTIME(starttime)))
Related
This question already has answers here:
Compare only day and month with date field in mysql
(5 answers)
Get month name from Date
(40 answers)
Closed 5 years ago.
I want to get date & month from date. example 2017-06-15 so I want date & month i.e. -06-15. Do u have any idea? Really appreciated.
From Below query I am getting month from date. but I want both date & month
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
SELECT DATE_FORMAT(date, '-%m-%d') AS month_name FROM table_name;
see the date_format documentation to see all the possible formats
MySQL MONTH() returns the MONTH for the date within a range of 1 to 12
( January to December). It Returns 0 when MONTH part for the date is
0.
SELECT MONTH('2009-05-18');
MySQL DAY() returns the day of the month for a specified date. The day
returned will be within the range of 1 to 31. If the given date is
‘0000-00-00’, the function will return 0. The DAYOFMONTH() is the
synonym of DAY().
SELECT DAY('2008-05-15');
Link : here
select month('2017-06-15'),DAY('2017-06-15')
you can use this function
DATEPART(datepart,date)
use below query
SELECT DATEPART(yyyy,datecolumn) AS dateYear,
DATEPART(mm,datecolumn) AS dateMonth,
DATEPART(dd,datecolumn) AS dateDay
FROM table_name
I happen to have ordered some data for years, I have a column over the years since the dates are not updated in the records. How could inquire to get me records only four years ago?
I tried this way:
$year=date ("Y"); //get the current year
$cons="SELECT iden,
ano,
wh,
so
FROM content WHERE wh='n'
AND ano BETWEEN 2013 AND".$year ."
ORDER BY ano desc, id DESC";
But the problem is that next year I have to modify the query to put 2014 instead of 2013. Had way to mount a conditional subtraction do?
Ano column I have: 2016, 2015, 2014, 2013, etc.
Let's see if I can lend a hand, because I have little knowledge of SQL and would like to learn more. Thank you.
Could you not just subtract 4 from your current year and use that as the first year?
$previous_year=date("Y")-4;
"...BETWEEN ".$previous_year." and ".$year."...."
I need a record whose year is just before the current year. My table schema and data is like this
aca_id|aca_start_date | aca_end_date
------|---------------|--------------
1 |2013-04-01 |2014-03-31
3 |2015-04-01 |2016-03-31
7 |2014-04-01 |2015-03-31
I want to fetch the record depending on the current year condition is - if current date year is 2014 then fetch the 1st record because its aca_start_date is 2013 which just before 2014. If current date year is 2015 then fetch the record 2nd record. I cannot sort the table on aca_start_date because table is sorted on aca_id in descending order for other purpose.
Need guidance how can I do this job in MySQL query or if this is possible to do by using php
Here is one way:
select t.*
from table t
where year(aca_start_date) = year(now()) - 1
limit 1;
Im am selecting various things from a table. The problem is I only want to select them if they belong to the current year and the next year.
My table looks like this
Title Text Date
The date is formated like this 0000-00-00 and is in the format "date"
So the question is how can i select only items from only this year and the next?
Example: the year is 2012, I have items in my table that is old and i dont want them to show - I only want to select items from at the first 2012 1 January and last in this case 31 Dec 2013 current year 2012 + 1 year.
Thanks a lot for any help!
SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(CURDATE())
OR
YEAR(date) = YEAR(CURDATE()) + 1
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
SELECT
*
FROM
table
WHERE
date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE())+1,'-12-31')
As ugly as it looks, it allows your query to use index on date field.
Better idea is to create limiting dates in external PHP script, so that the query can use query cache.
If you only want to show items, that are no older than two years, you can do this:
SELECT
*
FROM
table
WHERE
date >= CURDATE() - INTERVAL 2 YEAR;
Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";