简体   繁体   中英

How to count rows in MYSQL table with multiple column criteria?

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?

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();
   }

我认为您可以使用count()函数。请在表WHERE season = '12'AND type ='club'中找到以下查询SELECT COUNT(*);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM