Search code examples
mysqlcountrowmysql-num-rows

Counting rows in mysql database


I want to count from the row with the least value to the row with a specific value. For example,

Name / Point
--------------------
Pikachu  / 7
Voltorb / 1
Abra / 4
Sunflora / 3
Squirtle / 8
Snorlax / 12

I want to count to the 7, so I get the returned result of '4' (counting the rows with values 1, 3, 4, 7)

I know I should use count() or mysql_num_rows() but I can't think of the specifics. Thanks.


Solution

  • If you're working with MySQL, then you could ORDER BY Point:

    SELECT count(*) FROM table WHERE Point < 7 ORDER BY Point ASC
    

    If you want to know all about ORDER BY, check out the w3schools page: http://www.w3schools.com/sql/sql_orderby.asp

    Just in case you want to only count the rows based on the Point values:

    SELECT count(*) FROM table WHERE Point < 7 GROUP BY Point