简体   繁体   中英

Count total number of string matches for each name in MySQL

I am creating a prediction record table in HTML that displays the wins and losses in the format wins-losses for each predictor per month and total. The table below shows the expected outcome. I am using PHP with MySQL.

Predictor Name Monthly Prediction Record (wins/losses) Total Record (wins/losses)
Mookie Alexander 2-0 3-1
Zane Simon 1-2 2-3

I have combined 2 different MySQL tables using the following code

SELECT results.result_id, results.result, results.match_date, predictors.predictor_name
FROM results
INNER JOIN predictors ON results.predictor_id=predictors.predictor_id

To get this sample data

result_id result match_date predictor_name
1 Win 2021/01/01 Mookie Alexander
2 Loss 2021/01/01 Zane Simon
3 Loss 2021/07/01 Zane Simon
4 Loss 2021/07/01 Zane Simon
5 Loss 2021/01/01 Mookie Alexander
6 Win 2021/07/01 Mookie Alexander
7 Win 2021/07/01 Mookie Alexander
8 Win 2021/01/01 Zane Simon
9 Win 2021/07/01 Zane Simon

I am struggling to find a way to count how many wins each predictor (predictor_name)has, as well as how many losses they have.

Create Tables SQL

Matches

CREATE TABLE `matches` (
`match_id` int(50) NOT NULL AUTO_INCREMENT,
`match_name` varchar(50) NOT NULL,
`match_result` varchar(50) NOT NULL,
`match_specific_result` varchar(50) NOT NULL,
`match_super_specific_result` varchar(50) NOT NULL,
`match_date` date NOT NULL,
 PRIMARY KEY (`match_id`),
 KEY `match_name` (`match_name`),
 KEY `match_result` (`match_result`),
 KEY `match_specific_result` (`match_specific_result`),
 KEY `match_super_specific_result` (`match_super_specific_result`),
 KEY `match_date` (`match_date`)
 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

Predictions

CREATE TABLE `predictions` (
`prediction_id` int(50) NOT NULL AUTO_INCREMENT,
`predictor_id` int(50) NOT NULL,
`match_id` int(50) NOT NULL,
`prediction` varchar(50) NOT NULL,
`specific_prediction` varchar(50) NOT NULL,
`super_specific_prediction` varchar(50) NOT NULL,
PRIMARY KEY (`prediction_id`),
KEY `predictor_id` (`predictor_id`),
KEY `match_id` (`match_id`),
KEY `prediction` (`prediction`),
KEY `specific_predicion` (`specific_prediction`),
KEY `super_specific_prediction` (`super_specific_prediction`),
CONSTRAINT `predictions_ibfk_1` FOREIGN KEY (`predictor_id`) REFERENCES 
`predictors` (`predictor_id`),
CONSTRAINT `predictions_ibfk_2` FOREIGN KEY (`match_id`) REFERENCES 
`matches` (`match_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

Predictors

CREATE TABLE `predictors` (
`predictor_id` int(50) NOT NULL AUTO_INCREMENT,
`predictor_name` varchar(50) NOT NULL,
PRIMARY KEY (`predictor_id`),
KEY `predictor_name` (`predictor_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8,

Results

CREATE TABLE `results` (
`result_id` int(50) NOT NULL AUTO_INCREMENT,
`predictor_id` int(50) NOT NULL,
`prediction_Id` int(50) NOT NULL,
`prediction` varchar(50) NOT NULL,
`specific_prediction` varchar(50) NOT NULL,
`super_specific_prediction` varchar(50) NOT NULL,
`match_id` int(50) NOT NULL,
`match_result` varchar(50) NOT NULL,
`match_specific_result` varchar(50) NOT NULL,
`match_super_specific_result` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
`specific_result` varchar(50) NOT NULL,
`super_specific_result` varchar(50) NOT NULL,
`match_date` date NOT NULL,
PRIMARY KEY (`result_id`),
KEY `predictor_id` (`predictor_id`),
KEY `prediction_Id` (`prediction_Id`),
KEY `prediction` (`prediction`),
KEY `specific_prediction` (`specific_prediction`),
KEY `super_specific_prediction` (`super_specific_prediction`),
KEY `match_result` (`match_result`),
KEY `match_specific_result` (`match_specific_result`),
KEY `match_super_specific_result` (`match_super_specific_result`),
KEY `result` (`result`),
KEY `specific_result` (`specific_result`),
KEY `super_specific_result` (`super_specific_result`),
KEY `match_date` (`match_date`),
KEY `match_id` (`match_id`),
CONSTRAINT `results_ibfk_1` FOREIGN KEY (`predictor_id`) REFERENCES 
`predictors` (`predictor_id`),
 CONSTRAINT `results_ibfk_2` FOREIGN KEY (`match_id`) REFERENCES 
`matches` (`match_id`),
CONSTRAINT `results_ibfk_3` FOREIGN KEY (`prediction_Id`) REFERENCES 
`predictions` (`prediction_id`),
CONSTRAINT `results_ibfk_4` FOREIGN KEY (`prediction`) REFERENCES 
`predictions` (`prediction`),
CONSTRAINT `results_ibfk_5` FOREIGN KEY (`match_result`) REFERENCES 
`matches` (`match_result`),
CONSTRAINT `results_ibfk_6` FOREIGN KEY (`match_specific_result`) 
REFERENCES `matches` (`match_specific_result`),
CONSTRAINT `results_ibfk_7` FOREIGN KEY (`match_super_specific_result`) 
REFERENCES `matches` (`match_super_specific_result`),
CONSTRAINT `results_ibfk_8` FOREIGN KEY (`match_date`) REFERENCES 
`matches` (`match_date`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

PHP

 <?php

class Dbh {
private $servername;
private $username;
private $password;
private $dbname;

protected function connect() {
    $this->servername = "localhost";
    $this->username = "root";
    $this->password = "";
    $this->dbname = "prediction";

    $conn = new mysqli($this->servername, $this->username, $this- 
    >password, $this->dbname);
    return $conn;

}
}
?>


<?php
   class Predictors_Results extends Dbh {
   
    protected function getAllPredictors_Results() {
        $sql = "SELECT concat(sum(win),'-',sum(loss)) FROM (
        SELECT results.result_id, results.result, results.match_date, 
        predictors.predictor_name
        FROM results
        INNER JOIN predictors ON 
        results.predictor_id=predictors.predictor_id) as A
        group by predictor_name";
   
        $result = $this->connect()->query($sql);
        $numRows = $result->num_rows;
   
        if ($numRows > 0) {
            while ($row = $result->fetch_assoc()) {
                $data[] = $row;
            }
            return $data;
        }
    }
   }
?>

<?php
class ViewPredictors_Results extends Predictors_Results
{

    public function showAllPredictors_Results()
    {
        $datas = $this->getAllPredictors_Results();
        foreach ($datas as $data)
        {

            // Gets current date and date of the prediction
            $full_record_date = $data["match_date"];
            $record_date = substr($full_record_date, 0, -3);
            $current_date = date("Y-m");


            // If the current date matches the date of the prediction
            if ($current_date == $record_date)
            {

                $basicresult = mysql_query("SELECT * FROM results WHERE `result` = 'win'");
                $num_rows = mysql_num_rows($basicresult);
                echo $num_rows . " Rows\n";
            };

            //Post into table

            echo "<tr>
                     <td>" . $data['predictor_name'] . "</td>
                  </tr>";
        }
    }
}
?>

<table class="table-styling">
              <tr>
                <th>Predictor Name</th>
                <th>Latest Month Record</th>
                <th>Full Record</th>
              </tr>

              <?php

              $predictors_results = new ViewPredictors_Results();
              $predictors_results->showAllPredictors_Results();

              

              ?>
            </table>

If you provided us the table schema we could help you better. However suppose your result tables as A. You need use group by to do that.

Select concat(sum(win),'-',sum(loss)) as 'Total Record (wins/losses)' FROM 
(
SELECT results.result_id, results.result, results.match_date, predictors.predictor_name
FROM results
INNER JOIN predictors ON results.predictor_id=predictors.predictor_id
) as A
group by predictor_name

You can use match_date in group by to get monthly result.

EDIT==============

After providing your table schema, you can use something like below and then join this output with predictors to get the predictor name.

SELECT predictor_id,Concat(Sum(A.Wins),'-',Sum(A.Losses)) FROM (
SELECT predictor_id,count(1) as Wins,0 as Losses  FROM `results` WHERE result='win'
group by predictor_id
Union
SELECT predictor_id, 0 as Wins, count(1) as Losses  FROM `results` WHERE result='loss'
group by predictor_id
    ) as A
    group by predictor_id;

For monthly result use Month() function in mysql:

Select predictor_id,month,Concat(Sum(A.Wins),'-',Sum(A.Losses)) as 'Monthly Prediction Record (wins/losses)' FROM (
SELECT predictor_id,MONTH(match_date) as month,count(1) as Wins,0 as Losses  FROM `results` WHERE result='win'
group by predictor_id
Union
SELECT predictor_id,MONTH(match_date) as month, 0 as Wins, count(1) as Losses  FROM `results` WHERE result='loss'
group by predictor_id,MONTH(match_date) ) as A
group by predictor_id,month;

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