-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
  • 75,299
  • 16
  • 132
  • 159
  • 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
  • 75,299
  • 16
  • 132
  • 159
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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
insertusernamehere
  • 22,497
  • 8
  • 84
  • 119
1

Just use a regular count in the SQl query.

SELECT COUNT(*) FROM `yourTable` WHERE `X`=7;
Sirko
  • 69,531
  • 19
  • 142
  • 174
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
  • 29,943
  • 41
  • 156
  • 270
0
SELECT count(id) FROM table_name WHERE x='7'

But really, do a google search next time ^^

Naryl
  • 1,888
  • 1
  • 10
  • 12