如果来自 2 个不同 MYSQLi 列的 2 个不同字符串匹配,则使用 PHP 计算不同列的每个 ID

问题描述

我正在使用 PHP 创建一个表格来显示预测器的名称以及他们做出的成功预测和失败预测的数量

结果应该是这样的。

预测器名称 预测者记录(赢/输)
name1 0/0
name2 0/0

PHP 事实上,我对如何设置有点迷茫。

<table class="table-styling">
          <tr>
            <th>Predictor Name</th>
            <th>Full Record (Wins/Losses)</th>
          </tr> 

        <?PHP
        $sql = 'SELECT predictors.predictor_id,predictors.predictor_name,predictions.base_prediction,matches.base_result,predictions.specific_prediction,matches.specific_result,predictions.super_specific_prediction,matches.super_specific_result    
                FROM predictions 
                INNER JOIN matches ON matches.match_id = predictions.match_id
                INNER JOIN predictors ON predictors.predictor_id = predictions.predictor_id';

        $results = $MysqLi->query($sql);

        

        if($results->num_rows) {
          while ($row = $results->fetch_object()) {
            echo "<tr>
                    <td>$row->predictor_name</td> 
                    <td>$basePredictionWins/$basePredictionLosses</td>
                  </tr>";
          }

          if ($row->base_result === $row->base_prediction) {
            $basePredictionWins = array_count_values($results);
          }
          if ($row->base_result !== $row->base_prediction) {
            $basePredictionLosses = array_count_values($results);
          }

        }


        ?>

        </table>

主要问题似乎是这些 IF 语句

if ($row->base_result === $row->base_prediction) {
            $basePredictionWins = array_count_values($results);
          }
if ($row->base_result !== $row->base_prediction) {
            $basePredictionLosses = array_count_values($results);
          }

MysqLi 创建表

Matches

CREATE TABLE `matches` (

 `match_id` int(11) NOT NULL AUTO_INCREMENT,`match_date` date NOT NULL,`match_name` varchar(50) NOT NULL,`base_result` varchar(50) NOT NULL,`specific_result` varchar(50) NOT NULL,`super_specific_result` varchar(50) NOT NULL,`sport_id` int(11) NOT NULL,`organisation_id` int(11) NOT NULL,PRIMARY KEY (`match_id`),KEY `match_date` (`match_date`),KEY `match_name` (`match_name`),KEY `sport_id` (`sport_id`),KEY `organisation_id` (`organisation_id`),KEY `base_result` (`base_result`) USING BTREE,KEY `specific_result` (`specific_result`) USING BTREE,KEY `super_specific_result` (`super_specific_result`) USING BTREE,CONSTRAINT `matches_ibfk_1` FOREIGN KEY (`organisation_id`) REFERENCES `organisation` (`organisation_id`),CONSTRAINT `matches_ibfk_2` FOREIGN KEY (`sport_id`) REFERENCES `sports` (`sport_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

Predictors

CREATE TABLE `predictors` (

 `predictor_id` int(11) 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=latin1

Predictions

CREATE TABLE `predictions` (

 `prediction_id` int(11) NOT NULL,`match_id` int(11) NOT NULL,`predictor_id` int(11) NOT NULL,`base_prediction` varchar(50) NOT NULL,`specific_prediction` varchar(50) NOT NULL,`super_specific_prediction` varchar(50) NOT NULL,PRIMARY KEY (`prediction_id`),KEY `match_id` (`match_id`),KEY `predictor_id` (`predictor_id`),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 DEFAULT CHARSET=latin1

解决方法

使用您的 SQL,您想要 GROUP BY predictor_id,因此每行只有 1 个预测变量。之后,您可以使用 SQL SUM 将匹配结果相加。 SQL 中的 = 是一个比较,结果为 0 表示假,1 表示真。所以只要把这些加起来就可以得到总的赢/输。

SELECT predictors.predictor_id,predictors.predictor_name,SUM(matches.base_result = predictions.base_prediction) as WINS
       SUM(matches.base_result != predictions.base_prediction) as LOSSES  
FROM predictions 
INNER JOIN matches ON matches.match_id = predictions.match_id
INNER JOIN predictors ON predictors.predictor_id = predictions.predictor_id
GROUP BY predictions.predictor_id

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...