0

我有一个通过 PHP myadmin 返回 5 行的 mysql 查询,但是当我尝试通过 php 页面加载它时 - 我什么也没得到。

这是选择语句:

$result = mysqli_query($con,"select ID, MatchWon, MatchLost, MatchTied, (( MatchWon + (.5 * MatchTied ))/( MatchWon + MatchLost + MatchTied )) as WinPCT
FROM
(
SELECT ID, 
SUM ( CASE WHEN MatchResult = 'Won' THEN 1 ELSE 0 END ) MatchWon, 
SUM ( CASE WHEN MatchResult = 'Lost' THEN 1 ELSE 0 END ) MatchLost,
SUM ( CASE WHEN MatchResult = 'Tied' THEN 1 ELSE 0 END ) MatchTied
FROM 
(
    SELECT hometeam AS ID, 
    CASE WHEN homescore > visitorscore THEN 'Won'
    WHEN homescore < visitorscore THEN 'Lost'
    WHEN homescore = visitorscore THEN 'Tied'
    END AS MatchResult
    FROM scores

    UNION ALL   
    SELECT visitingteam AS ID, 
    CASE WHEN homescore < visitorscore THEN 'Won'
    WHEN homescore > visitorscore THEN 'Lost'
    WHEN homescore = visitorscore THEN 'Tied'
    END AS MatchResult
    FROM scores
) as wonlosttable
GROUP BY ID ) as countwinlosstable
ORDER BY WinPCT desc, MatchWon desc, MatchLost asc, MatchTied asc
");

这是我用来显示的 php 代码:

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td align=center>" . $row['ID'] . "</td>";
  echo "<td align=center>" . $row['MatchWon'] . "</td>";
  echo "<td align=center>" . $row['MatchLost'] . "</td>";
  echo "<td align=center>" . $row['MatchTied'] . "</td>";
  echo "</tr>";
  }

如果需要测试,这是简单的表创建代码:

CREATE TABLE `scores` (
  `gameid` int(11) NOT NULL auto_increment,
  `datesubmitted` varchar(50) NOT NULL,
  `gamedate` varchar(11) NOT NULL,
  `gametime` time NOT NULL,
  `visitingteam` varchar(50) NOT NULL,
  `visitorscore` int(11) NOT NULL,
  `hometeam` varchar(50) NOT NULL,
  `homescore` int(11) NOT NULL,
  `submitter` varchar(50) NOT NULL,
  PRIMARY KEY  (`gameid`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

-- 
-- Dumping data for table `scores`
-- 

INSERT INTO `scores` VALUES (23, '7/30/2013 9:33:01 AM', '7/24/2013', '17:00:00', 'Culvers', 8, 'Siebe Studios', 2, 'MAXWELL');
INSERT INTO `scores` VALUES (26, '7/30/2013 9:58:37 AM', '7/24/2013', '18:00:00', 'Jimmy Johns', 10, 'Home Technology Systems', 3, 'MAXWELL');
INSERT INTO `scores` VALUES (25, '7/30/2013 9:47:52 AM', '7/29/2013', '15:00:00', 'Jimmy Johns', 10, 'Home Technology Systems', 3, 'MAXWELL');
INSERT INTO `scores` VALUES (24, '7/30/2013 9:46:34 AM', '7/26/2013', '10:30:00', 'Ellis', 5, 'Happy Joes', 13, 'MAXWELL');
4

1 回答 1

1

你的 SUM 他们有空格。

改变这个

     SUM ( CASE WHEN MatchResult = 'Won' THEN 1 ELSE 0 END ) MatchWon, 
     SUM ( CASE WHEN MatchResult = 'Lost' THEN 1 ELSE 0 END ) MatchLost,
     SUM ( CASE WHEN MatchResult = 'Tied' THEN 1 ELSE 0 END ) MatchTied
        ^^-----//--spaces here

     SUM( CASE WHEN MatchResult = 'Won' THEN 1 ELSE 0 END ) MatchWon, 
     SUM( CASE WHEN MatchResult = 'Lost' THEN 1 ELSE 0 END ) MatchLost,
     SUM( CASE WHEN MatchResult = 'Tied' THEN 1 ELSE 0 END ) MatchTied
于 2013-07-30T21:08:50.820 回答