English 中文(简体)
我怎样才能在MISQL每年每个月查询?
原标题:How can I make a query for each month of each year in MYSQL?
  • 时间:2012-05-23 21:18:27
  •  标签:
  • php
  • mysql

我需要提出一个查询,让每个月每个月的字段对应该月的字段

像这样的东西:

                      field 1                                field 2 

       count_of_fields_that_corresponds_this_month, same_fields_but_with_value_true
       --------------------------------------------|--------------------------------
january                                            |
       --------------------------------------------|-------------------------------
february                                           |
       --------------------------------------------|--------------------------------
march                                              |
       --------------------------------------------|--------------------------------
april                                              |
       --------------------------------------------|--------------------------------
etc...                                             |

在这个例子中,我有2个字段 我需要填入表格, 但实际上我还有很多, 但我不知道如何在一个查询中获得每个月的字段, 我试过做同样的事情,但有很多查询, 我认为这是一个错误的做法, 所以,你能建议我如何做到这一点吗? 或者我需要用什么功能来做我的智囊团?

我想每个月拿一,一,二,三,等等。 如果每年也有可能的话,非常感谢。

$this->db->query("SELECT count(*) AS count FROM `simple` WHERE `ochistka` =  somevalue  AND MONTH(`date`) = 01");
$this->db->query("SELECT count(*) AS count FROM `simple` WHERE `ochistka` =  somevalue  AND MONTH(`date`) = 01 AND `prime4anie` =  somevalue ");

所以,在这个查询中,我正在得到那个字段, 但仅一个月, 我怎么能改进它, 并用一个镜头( 询问) 每个月进行查询?

最佳回答
SELECT YEAR(datefield), MONTH(datefield), COUNT(field1), COUNT(field2)
FROM table
GROUP BY YEAR(datefield), MONTH(datefield)

设置您自己的字段和表格名称

问题回答

删除查询的月份部分,您将得到一系列结果。然后,您可以通过调用来添加一组来排序它们。

SELECT count(*) AS count FROM `simple` WHERE `ochistka` =  somevalue  GROUP BY MONTH




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签