gyogyo0101
gyogyo0101

Reputation: 61

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.

Upvotes: 1

Views: 245

Answers (3)

Stephen
Stephen

Reputation: 91

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

Upvotes: 1

Ravi Jain
Ravi Jain

Reputation: 1482

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

select count(*) from table where Point >= least_value and Point<= max_value 

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

I think you want this :

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

Count(*) counts all rows in a set.

Upvotes: 2

Related Questions