如何从mysql表字段格式2013-01-01中选择年份和月份的不同值

如何从mysql表字段格式2013-01-01中选择年份和月份的不同值

问题描述:

I have the following data table

"id","date_occurred","country","town","quantity"
"1","2012-06-01","England","Andover","82"
"2","2012-06-01","England","Colchester","569"
"3","2012-06-01","England","Farnham","1"
"4","2012-06-01","England","England","4"
"5","2012-06-01","England","America","13"
"6","2012-06-01","America","England","114"
"7","2012-06-02","England","Andover","4"
"8","2012-06-02","England","Colchester","207"
"9","2012-06-02","America","England","14"
"10","2012-06-03","England","Andover","3"
"11","2012-06-03","England","Colchester","72"
"12","2012-06-03","England","America","1"
"13","2012-06-03","America","England","15"
"14","2012-07-04","England","Andover","1"
"15","2012-07-04","England","Colchester","309"
"16","2012-07-04","England","America","4"
"17","2012-07-04","America","England","11"
"18","2012-08-05","England","Andover","2"
"19","2012-08-05","England","Colchester","319"
"20","2012-08-05","England","Farnham","1"
"21","2012-08-05","England","America","4"
"22","2012-08-05","America","England","25"
"23","2012-08-06","England","Andover","93"
"24","2013-06-06","England","Colchester","542"
"25","2013-06-06","England","Farnham","1"
"26","2013-06-06","England","England","4"
"27","2013-06-06","England","America","7"
"28","2013-06-06","America","England","115"

When I run the standalone query below I produce the result I need.

Total sales per day per country for a given month query:

SELECT date_occurred, country, SUM(quantity)
FROM tableA 
WHERE YEAR(date_occurred) = 2013 AND MONTH(date_occurred) = 6
GROUP BY date_occurred, country

Total sales per day per country for a given month

2012-06-01 England 669
2012-06-01 America 114
2012-06-02 England 211 

I have created an ajax dropdown to query the database with three select boxes

enter image description here
Question

My Ajax script produces 3 variables
$date_o $country $town

via $_POST

How do I use the variables alone to create the query above?

What I have tried

The query below uses the $country variable and works.

 SELECT date_occurred, country, SUM(quantity) AS quantity
 FROM TableA
 WHERE YEAR(date_occurred) =2013
 AND MONTH(date_occurred) = 6 AND country ='$country'
 GROUP BY date_occurred , country = '$country'

however I have tried many permutations to use only the $_POSTED values to produce the query but have been unsuccessful.

**The required out come is something like below. **

 SELECT $date_o, $country, SUM(quantity) AS quantity
 FROM TableA
 WHERE YEAR($date_o) = year_part_of_$date_o
 AND MONTH($date_o) = month_part_of_$date_o AND country ='$country'
 GROUP BY $date_o , country = '$country'

I think i need to strip the month and year bits out of the $date_o variable but cant work it out. Any simple answers or advice welcome! Thanks in advance.

$year = substr($date_o, 0, 4);
$month = substr($date_o, 5, 2);

If you do not need leading zeroes in month then

$month = ltrim(substr($date_o, 5, 2), '0');

You may try

$year = date('Y',strtotime($date_o));
$month = date('n',strtotime($date_o));

This can give the output as 2013, 6 (not 06: If you need 06 place 'm' in the place of 'n')