dongyu4863 2014-01-06 11:08
浏览 85
已采纳

如何从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.

  • 写回答

2条回答 默认 最新

  • dqy012345 2014-01-06 11:20
    关注
    $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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。