如何获取给定日期之间每天的行数?

时间:2022-05-31 09:11:55

I am having table in the below format. I need mysql query to get the no of rows perday when i give date between 2008-10-12 and 2008-10-13

我有以下格式的表格。当我在2008-10-12和2008-10-13之间给出日期时,我需要mysql查询来获取每天的行数

 Name | KW     | KV   | I    | Date                |
------+--------+------+------+---------------------+
 UPS1 | 353.50 | NULL | NULL | 2008-10-12 00:54:36 |
 UPS1 | 352.50 | NULL | NULL | 2008-10-12 01:54:36 |
 UPS1 | 351.90 | NULL | NULL | 2008-10-12 02:54:36 |
 UPS1 | 351.60 | NULL | NULL | 2008-10-12 03:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 04:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 05:54:36 |
 UPS1 | 351.90 | NULL | NULL | 2008-10-12 06:54:36 |
 UPS1 | 352.50 | NULL | NULL | 2008-10-12 07:54:36 |
 UPS1 | 352.50 | NULL | NULL | 2008-10-12 08:54:36 |
 UPS1 | 353.20 | NULL | NULL | 2008-10-12 09:54:36 |
 UPS1 | 353.50 | NULL | NULL | 2008-10-12 10:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 11:54:36 |
 UPS1 | 352.50 | NULL | NULL | 2008-10-12 12:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 13:54:36 |
 UPS1 | 353.20 | NULL | NULL | 2008-10-12 14:54:36 |
 UPS1 | 353.50 | NULL | NULL | 2008-10-12 15:54:36 |
 UPS1 | 352.90 | NULL | NULL | 2008-10-12 16:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 17:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 18:54:36 |
 UPS1 | 352.90 | NULL | NULL | 2008-10-12 19:54:36 |
 UPS1 | 352.20 | NULL | NULL | 2008-10-12 20:54:36 |
 UPS1 | 352.50 | NULL | NULL | 2008-10-12 21:54:36 |
 UPS1 | 352.90 | NULL | NULL | 2008-10-12 22:54:36 |
 UPS1 | 353.20 | NULL | NULL | 2008-10-12 23:54:36 |
 UPS1 | 355.80 | NULL | NULL | 2008-10-13 00:54:36 |
 UPS1 | 358.40 | NULL | NULL | 2008-10-13 01:54:36 |
 UPS1 | 358.00 | NULL | NULL | 2008-10-13 02:54:36 |
 UPS1 | 359.00 | NULL | NULL | 2008-10-13 03:54:36 |
 UPS1 | 357.70 | NULL | NULL | 2008-10-13 04:54:36 |
 UPS1 | 357.40 | NULL | NULL | 2008-10-13 05:54:36 |
 UPS1 | 357.40 | NULL | NULL | 2008-10-13 06:54:36 |
 UPS1 | 359.00 | NULL | NULL | 2008-10-13 07:54:36 |
 UPS1 | 357.10 | NULL | NULL | 2008-10-13 08:54:36 |
 UPS1 | 359.00 | NULL | NULL | 2008-10-13 09:54:36 |
 UPS1 | 357.70 | NULL | NULL | 2008-10-13 10:54:36 |
 UPS1 | 357.40 | NULL | NULL | 2008-10-13 11:54:36 |
 UPS1 | 357.40 | NULL | NULL | 2008-10-13 12:54:36 |
 UPS1 | 359.00 | NULL | NULL | 2008-10-13 13:54:36 |
 UPS1 | 357.10 | NULL | NULL | 2008-10-13 14:54:36 |
 UPS1 | 358.00 | NULL | NULL | 2008-10-13 15:54:36 |
 UPS1 | 359.30 | NULL | NULL | 2008-10-13 16:54:36 |
 UPS1 | 357.10 | NULL | NULL | 2008-10-13 17:54:36 |
 UPS1 | 358.40 | NULL | NULL | 2008-10-13 18:54:36 |
 UPS1 | 357.70 | NULL | NULL | 2008-10-13 19:54:36 |
 UPS1 | 359.00 | NULL | NULL | 2008-10-13 20:54:36 |
 UPS1 | 358.70 | NULL | NULL | 2008-10-13 21:54:36 |
 UPS1 | 358.70 | NULL | NULL | 2008-10-13 22:54:36 |
 UPS1 | 358.40 | NULL | NULL | 2008-10-13 23:54:36 |

5 个解决方案

#1


I have changed the query so that Date column case matches exactly what you have in your query results.

我更改了查询,以便Date列大小写完全匹配查询结果中的内容。

select Date, count(Date)
from TABLE
where Date >= '2008-10-12' and Date <= '2008-10-13'
group by Date

You will need to change TABLE in the from clause, but everything else should be correct for your table.

您需要在from子句中更改TABLE,但其他所有内容对于您的表都应该是正确的。

edit:

I have a table with a similar structure and I changed a date column to be named 'Date'. The following worked for me.

我有一个类似结构的表,我更改了一个名为'Date'的日期列。以下对我有用。

mysql> select Date, count(Date) 
    from calendar_items 
    where date between '2008-12-12' and '2008-12-13' 
    group by Date;

+------------+-------------+
| Date       | count(Date) |
+------------+-------------+
| 2008-12-12 |          14 | 
| 2008-12-13 |           6 | 
+------------+-------------+
2 rows in set (0.00 sec)

mysql> 

#2


Shouldn't it be:

不应该是:

select date, count(date) where date >= '2008-10-12' and date <= '2008-10-13' group by date

选择日期,计数(日期),其中日期> ='2008-10-12'和日期<='2008-10-13'按日期分组

#3


Please take a look for fetching records per day..you can use like this---> SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;

请查看每天获取记录..您可以像这样使用---> SELECT FROM FROM tbl_name WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY)<= date_col;

#4


Regarding your syntax error: As "date" is a reserved term in mysql, u have to escape it in your queries.

关于语法错误:因为“date”是mysql中的保留术语,所以你必须在查询中转义它。

With this in mind ewalshes query should work:

考虑到这一点ewalshes查询应该工作:

select `Date`, count(`Date`)
from TABLENAME
where `Date` between '2008-10-12' and '2008-10-13'
group by `Date`

#5


I'd try this:

我试试这个:

SELECT count( * ) , date( `date` )
FROM `myTableName`
GROUP BY date( `date` )

(Tested on MySql 4.1)

(在MySql 4.1上测试过)

#1


I have changed the query so that Date column case matches exactly what you have in your query results.

我更改了查询,以便Date列大小写完全匹配查询结果中的内容。

select Date, count(Date)
from TABLE
where Date >= '2008-10-12' and Date <= '2008-10-13'
group by Date

You will need to change TABLE in the from clause, but everything else should be correct for your table.

您需要在from子句中更改TABLE,但其他所有内容对于您的表都应该是正确的。

edit:

I have a table with a similar structure and I changed a date column to be named 'Date'. The following worked for me.

我有一个类似结构的表,我更改了一个名为'Date'的日期列。以下对我有用。

mysql> select Date, count(Date) 
    from calendar_items 
    where date between '2008-12-12' and '2008-12-13' 
    group by Date;

+------------+-------------+
| Date       | count(Date) |
+------------+-------------+
| 2008-12-12 |          14 | 
| 2008-12-13 |           6 | 
+------------+-------------+
2 rows in set (0.00 sec)

mysql> 

#2


Shouldn't it be:

不应该是:

select date, count(date) where date >= '2008-10-12' and date <= '2008-10-13' group by date

选择日期,计数(日期),其中日期> ='2008-10-12'和日期<='2008-10-13'按日期分组

#3


Please take a look for fetching records per day..you can use like this---> SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;

请查看每天获取记录..您可以像这样使用---> SELECT FROM FROM tbl_name WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY)<= date_col;

#4


Regarding your syntax error: As "date" is a reserved term in mysql, u have to escape it in your queries.

关于语法错误:因为“date”是mysql中的保留术语,所以你必须在查询中转义它。

With this in mind ewalshes query should work:

考虑到这一点ewalshes查询应该工作:

select `Date`, count(`Date`)
from TABLENAME
where `Date` between '2008-10-12' and '2008-10-13'
group by `Date`

#5


I'd try this:

我试试这个:

SELECT count( * ) , date( `date` )
FROM `myTableName`
GROUP BY date( `date` )

(Tested on MySql 4.1)

(在MySql 4.1上测试过)