-7

I want get count of rows from my table where X == 7

+----+----------+-------+---+   
| id | username | email | X |
+----+----------+-------+---+
|  1 |          |       | 7 |
|  2 |          |       | 7 |
|  3 |          |       | 7 |
|  4 |          |       |   |
|  5 |          |       |   |
+----+----------+-------+---+

There are 3 rows where X == 7

How can i get the number of those rows?

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • 1
    select count(*) as total from table where x=7 some thing like this! –  Feb 13 '13 at 12:05

5 Answers5

3
SELECT COUNT(id) FROM yourtable where x = 7;

SQL Fiddle Demo

this will give you one value:

| COUNT(ID) |
-------------
|         3 |

If you want to do that for all the values of x's, add a GROUP BY:

SELECT x, COUNT(id) TheCount
FROM yourtable
GROUP BY x;

Updated SQL Fiddle Demo

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
3
$sql = "SELECT count(*) FROM `table` WHERE X = 7"; 
$result = $con->prepare($sql); 
$result->execute(); 
$number_of_rows = $result->fetchColumn();

From Count with PDO

As a note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
1

Just use a regular count in the SQl query.

SELECT COUNT(*) FROM `yourTable` WHERE `X`=7;
Sirko
  • 72,589
  • 19
  • 149
  • 183
1

if column x is of type INT use

SELECT COUNT(1) FROM tableName WHERE X=7

if column x is of type varchar use

SELECT COUNT(1) FROM tableName WHERE X='7'

I believe you have x column as integer...

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0
SELECT count(id) FROM table_name WHERE x='7'

But really, do a google search next time ^^

Naryl
  • 1,878
  • 1
  • 10
  • 12