0

I have a table to football results in a mysql table and want to count how many games have been played by season and game type.

For example the table looks like this

+---------------+-------------+----------+
|   Type        |  Season     |   Goals  |
+---------------+-------------+----------+
|   Club        |    11       |    4     |
+---------------+-------------+----------+
|International  |    12       |    5     |
+---------------+-------------+----------+
|   Club        |    12       |     0    |
+---------------+-------------+----------+

What I want to do is total the number of club games played in season 12 so that would result in 1.

the current MYSQL code I have is

"SELECT COUNT * FROM table WHERE season='12' AND type='club'"

and that isn't working as I think I am using the wrong function. What is the correct function for the desired output?

4
  • What should be your output? Commented Mar 7, 2014 at 10:18
  • 1
    Hello Aasim, are the results incorrect or you get an error? If you are getting an error you should use "COUNT(*)" instead of "COUNT *". If you do not get an error, can you specify what data you get with the above SQL statement? Commented Mar 7, 2014 at 10:18
  • I get a syntax error for both * and (*) Commented Mar 7, 2014 at 10:21
  • you are running this in php or in database ? Commented Mar 7, 2014 at 10:31

3 Answers 3

1

try this

 SELECT COUNT(*) total FROM `<tablename>` WHERE season=12 AND type='club';

make sure your connection established:

 $sql = "SELECT COUNT(*) total FROM cristiano WHERE season=12 AND which='club'"
 if ($result=mysql_query($sql)) {
    while($row=mysql_fetch_row($result)) {
     echo $row['total'];
    }
 } else {
   echo "Error" . mysql_error();
   }
Sign up to request clarification or add additional context in comments.

4 Comments

you need to provide your table name whatever it is ok
try again and let me know if error comes again ,, also remove quotes around season number
its not returning any errors now but how would I echo the result? For a SUM function I hade $goals = $row['sum']; and then would echo $goal in a div.
$q = mysql_query("SELECT COUNT(*) FROM cristiano WHERE season=12 AND which='club';") or die(mysql_error()); $row = mysql_fetch_assoc($q); $games = $row['count'];
0

I think you use the count() function it will work.Please find the below query SELECT COUNT(*) FROM table WHERE season='12' AND type='club';

Comments

0

here Table name is 'table' and type-column name is 'type' - These both words 'table' and 'type' are reserved words, so while executing the query , write the query as:

SELECT COUNT(*) FROM `table` WHERE `season`=12 AND `type`='club';

use ` sign as used in above code-line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.