php mysql date extract with distinct - php

in mysql table i have the field 'date' which has the data as
2012-08-02 02:33:26
2012-08-02 05:33:26
2012-07-02 06:33:26
2012-06-02 01:33:26
2012-06-10 09:33:26
2011-05-10 10:33:26
2011-04-10 02:33:26
like that
i want to get the output as
2012-08
2012-07
2012-06
2011-05
2011-04
ie get the unique year and month (distinct of year and month of the date field)
I used the following query
SELECT distinct(YEAR(date)) FROM table
It returns the year only. i want to get the output as defined above. is there any possibility to get the out put like this. How do i write the select query.
Please help to this.

You might want to try this:
SELECT DISTINCT DATE_FORMAT(colName, '%Y-%m')
FROM tableName
SEE HERE: Date_Format( )

SELECT DISTINCT DATE_FORMAT(`date`, '%Y-%m') FROM table
you can get full help from here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

suppose you are getting like this from Database:-
$row['date']=2012-08-02 02:33:26;
then you can use explode function:-
$var=explode(" ",$row['date']);
$date=$var[0];//here you will get 2012-08-02
$res=explode("-",$date);
$res1=$res[0];//you will get 2012
$res2=$res[1];//you will get 08
$res3=$res[2];//you will get 02
now the final result:-
$final=$res1."-".$res2;//you will get 2012-08
this is an alternate answer if you don't want to handle this on MySQL end so you can handle this by using PHP concepts.

Related

Get data from the current month

I have a question. So I need to get data from the last days of months using a sql request. For example :
Now date is 22/08/2016, I need to get data from 01/08/2016-22/08/2016;
Now date is 04/06/2016, I need to get data from 01/06/2016-04/06/2016;
Sorry but I dont have an idea. Thx in advance and sorry for my english
I would do something like this:
SELECT <table-columns> FROM <table-name>
WHERE (<date-column> BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
This DATE_FORMAT(NOW() ,'%Y-%m-01') will return the first date of your current month and year.
Need to use something like:
YEAR(time) = YEAR(NOW())
AND MONTH(time) = MONTH(NOW())
You are looking for something like this:
SELECT * FROM tbl_name WHERE yourDate.MONTH()=NOW().MONTH();
Refine the syntax from here.
You need to get the first day of the month for a given date then, in order to make your comparison with the database:
select date(concat_ws('-',year(curdate()),lpad(month(curdate()),2,'00'),'01')); gives you that (for curdate() here)

Get Year from complete data filter year

I have a data in my db, like:
2016-05-17 , 2015-05-20 , 2015-02-02
I want to get only the years to create a list, like:
2016 , 2015
I'm making this:
SELECT * FROM insercao GROUP BY SUBSTR(data,0,4)
But this return only:
2015
How can I return All not duplicated years?
select distinct EXTRACT(YEAR FROM date_column_name) FROM date_extract;
The extract keyword of MYSQL extracts years form the date format that you mentioned in question.
Distinct will only select non repeating years.
If data is a datetime field, you ahve to change your query in this way:
SELECT * FROM insercao GROUP BY YEAR(`data`)
Edit:
In fact, to return only year value, you have to act in this way:
SELECT YEAR(`time`) as year FROM insercao GROUP BY year

How to get the number of persons joined per date in a month?

I inserted join date for every employee in a table like March 9, 2014, 10:50:29
After the completion of month ,I need to get the data like
march 9 => 20,
march 10 =>30, .....
I tried with group by statements along with count(*) , but i didn't got my required output. please help me...Thanks in advance..
I suppose that you are using datetime field for storing the DateOfJoining.
Use this query, (Please replace table-name and fields as per yours)
SELECT DAY(joining_date) AS jdate, COUNT(*) AS jtotal
FROM your_table
WHERE MONTH(joining_date) = 3
GROUP BY jdate;
Hope it works.
SELECT DAY(joining_date) AS jdate, COUNT(*) AS jtotal
FROM your_table
WHERE MONTH(joining_date)=3 AND YEAR(joining_date)=2014
GROUP BY DAY(joining_date);
should work

jQuery flot, MySQL count rows for each day

I've table for hits that contains of 2 fields
id time(TIMESTAMP)
1 2012-05-03 08:56:17
2 2012-04-26 03:22:20
3 2012-05-03 08:56:17
How can I count how many rows for each day for example for the past 10 days?
I mean what is the right query for it that produces:
2012-05-03 => 25 hits
2012-05-05 => 55 hits
etc
Also, how can i arrange the returned array as jQuery flot array ?
any answers / articles regarding my questions would be great help
Thank you
You want to use a Group By on the date field
something like
select count(*), dateField from tablename
group by dateField
Here is how I'd do it:
select count(id), DATE(time) as hitday from yourtable group by hitday
And regarding your jQuery thing, I am not very sure if I understand it or not. You want mysql to generate a jQuery parsable array for you? For that you definitely need to have a kinda middle man like php.
Here is another thread which shows you how a select result is converted to json using php.

want to fetch only the month part from a date in mysql

Is it possible to fetch only the month part from a sql date entry?
like the date is stored like '2011-01-06'. if i want to fetch only the '01' portion how can I do that?
thanxx in advance.
You can use DATE_FORMAT to do it, like this:
SELECT DATE_FORMAT(DateField, '%m') FROM table
you can do it in mysql
like :
select DATE(date_field) from mytable
from manual :
MONTH(date)
Returns the month for date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero month part.
mysql> SELECT MONTH('2008-02-03');
-> 2
in a mysql query, you can just do this:
MONTH(date_field)
mysql> SELECT MONTH('2008-02-03');
Result: 2
You can use the MONTH function of MySQL:
SELECT MONTH('2011-01-06');
Result:
1
To get month name instead, you can use MONTHNAME like this:
SELECT MONTHNAME('2011-01-06');
Result:
January
You may want to try
select substring(`mydate_field`,6,2) from `table_name`;
In ANSI-SQL you can use EXTRACT:
SELECT
EXTRACT('month' FROM fieldname)
FROM
tablename;
This works in many databases, MySQL as well.

Categories