Mysql Query for counting rows with specific days difference

Homnath Bagale

Going to make a line graph. I want to query count with difference of 5 days. Suppose if I have following rows:

 booking_date
 2015-02-1
 2015-02-3
 2015-02-5
 2015-02-6
 2015-02-6
 2015-02-9
 2015-02-10
 2015-02-15
 2015-02-17
 2015-02-23
 2015-02-28

In above table column it contains date. Now How can I do mysql query so that it can return with difference of 5 days like:

 1 => 3  // count of date between 2015-02-1 & 2015-02-05 is 3
 2 => 4  // count of date between 2015-02-06 & 2015-02-10 is 4
 3 => 1  // count of date between 2015-02-11 & 2015-02-15 is 1
 4 => 1  // count of date between 2015-02-16 & 2015-02-20 is 1
 5 => 1  // count of date between 2015-02-21 & 2015-02-25 is 1
 6 => 1  // count of date between 2015-02-26 & 2015-02-30 is 1

Is any direct way to query like above. I am not so good at mysql. But can do php nicely.

Pinx0

You can do the following to get everything in the same query.

First of all get the unix timestamp for the date you want to start grouping in 5 days. In your example that would be 2015-02-01 -> 1422748800

Then the query would be the following:

SELECT COUNT(*), FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5)) as FiveDayPackNumber from tbl GROUP BY FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5))

Haven't tested it so it may require some tweaking but you can get the idea: It will group them by the number of 5-days-packs that passed since your initial date, starting at 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Counting specific rows mysql + express

From Dev

Trouble counting rows with specific value - Doctrine, Symfony2, mysql

From Dev

counting duplicate rows in mysql

From Dev

MySQL query for last rows that have a specific field

From Dev

Counting specific values in rows in CSV

From Dev

Counting number of rows in Query with PHP

From Dev

Counting distinct days from DateTime in MySQL

From Dev

MySQL COUNT(*) not counting result rows

From Dev

difference between specific rows

From Dev

difference between null and 0 while counting rows

From Dev

MySQL query counting on multiple tables

From Dev

PHP function for counting individual rows inside the (MySql) query loop returning null

From Dev

PHP function for counting individual rows inside the (MySql) query loop returning null

From Dev

How to find the difference of two rows in an MySQL query result

From Dev

PHP query difference between 2 rows MySql table

From Dev

mysql: slightest difference between days

From Dev

How to query rows between specific date range in MySQL efficiently?

From Dev

how to get a specific id within 5 rows in a paging query in Mysql

From Dev

How to show the rows of this specific mySQL table with a single query and PHP?

From Dev

how to update all rows based on specific date by a single mysql query

From Dev

counting the amount of rows returned with a query in laravel

From Dev

Counting rows from different tables in same query

From Dev

Counting rows from a GROUP BY query using an index

From Dev

Counting values for multiple distinct rows in a SQL Query

From Dev

MySQL query to group timestamps by days

From Dev

What to count when counting all rows MySQL

From Dev

Counting/totaling rows in a create view statement, mysql

From Dev

What to count when counting all rows MySQL

From Dev

MYSQL counting rows until last different row

Related Related

  1. 1

    Counting specific rows mysql + express

  2. 2

    Trouble counting rows with specific value - Doctrine, Symfony2, mysql

  3. 3

    counting duplicate rows in mysql

  4. 4

    MySQL query for last rows that have a specific field

  5. 5

    Counting specific values in rows in CSV

  6. 6

    Counting number of rows in Query with PHP

  7. 7

    Counting distinct days from DateTime in MySQL

  8. 8

    MySQL COUNT(*) not counting result rows

  9. 9

    difference between specific rows

  10. 10

    difference between null and 0 while counting rows

  11. 11

    MySQL query counting on multiple tables

  12. 12

    PHP function for counting individual rows inside the (MySql) query loop returning null

  13. 13

    PHP function for counting individual rows inside the (MySql) query loop returning null

  14. 14

    How to find the difference of two rows in an MySQL query result

  15. 15

    PHP query difference between 2 rows MySql table

  16. 16

    mysql: slightest difference between days

  17. 17

    How to query rows between specific date range in MySQL efficiently?

  18. 18

    how to get a specific id within 5 rows in a paging query in Mysql

  19. 19

    How to show the rows of this specific mySQL table with a single query and PHP?

  20. 20

    how to update all rows based on specific date by a single mysql query

  21. 21

    counting the amount of rows returned with a query in laravel

  22. 22

    Counting rows from different tables in same query

  23. 23

    Counting rows from a GROUP BY query using an index

  24. 24

    Counting values for multiple distinct rows in a SQL Query

  25. 25

    MySQL query to group timestamps by days

  26. 26

    What to count when counting all rows MySQL

  27. 27

    Counting/totaling rows in a create view statement, mysql

  28. 28

    What to count when counting all rows MySQL

  29. 29

    MYSQL counting rows until last different row

HotTag

Archive