How can I retrieve records and a total count of the records - MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Corwin Moyne
    New Member
    • Feb 2012
    • 37

    How can I retrieve records and a total count of the records - MySQL

    If I write:

    SELECT * FROM table_name WHERE date BETWEEN "2014/07/01" AND "2014/07/04"

    I get 4 records returned which is what I want. But I also want the total number of records which in this case is 4, but if I write:

    SELECT *, count(*) AS total FROM table_name WHERE date BETWEEN "2014/07/01" AND "2014/07/04"

    It displays total 4, but only one record.

    How can I display all 4 records and "total 4"?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    The short answer is:
    Code:
    SELECT t.*, (SELECT count(*) 
                 FROM table_name
                 WHERE date BETWEEN "2014/07/01" AND "2014/07/04") as Count
    FROM table_name t
    WHERE t.date BETWEEN "2014/07/01" AND "2014/07/04"

    Comment

    Working...