1

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.

gyogyo0101
  • 61
  • 1
  • 2
  • 10

3 Answers3

2

I think you want this :

 select count(*) from mytable where Point<=7;

Count(*) counts all rows in a set.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

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
Stephen
  • 91
  • 3
  • "row with the least value to the row with a specific value" makes me think that he wants to make sure that there is some order in the Point values, not just in this specific case where they properly align. – Stephen Jun 11 '12 at 18:07
  • 2
    If you return only the row count, the order is useless. – Denys Séguret Jun 11 '12 at 18:07
1

This may help you to get rows falling between range of values :

select count(*) from table where Point >= least_value and Point<= max_value 
Ravi Jain
  • 1,452
  • 2
  • 17
  • 41