我想要:
计算有多少行具有列date = $ date
秒计数列日期= $ date和列结果=单词'win'的行数
然后将第一个计数值除以第二个,然后回显结果/值。
该值将重新显示为1-当行数= 3时,结果列中有Win的行数= 2时,结果应为1.5。
当我甚至回显“”。$ testrow ['win_count']时。 “”;结果仍然是3,这是错误的,那就是总行数的结果。
<?php
ini_set('display_errors', 1); error_reporting(E_ALL ^ E_NOTICE);
$connection = mysqli_connect("*****", "*****", "*****", "*****");
if (!$connection) {
die("Database connection failed: " . mysqli_connect_error());
}
$Date = '2019-03-11';
$Win = 'Win';
$testsql="
SELECT
count(*) AS bet_count,
count(IF(result ='$Win', 1, 0)) AS win_count
FROM bets WHERE betDate = '$Date' GROUP BY betDate
";
$testresult = mysqli_query($connection, $testsql);
while ($testrow = mysqli_fetch_assoc($testresult))
{
echo "<tr>";
echo "<td class='text-center'>".($testrow['bet_count']/$testrow['win_count']). "</td>";
echo "</tr>";
}
mysqli_close($connection);
?>
答案 0 :(得分:0)
这个......
count(IF(result = '$Win', 1, 0)) AS win_count
应该是......
count(IF(result = '$Win', 1, NULL)) AS win_count
或...
count(CASE WHEN result = '$Win' THEN 1 END)) AS win_count
我也建议您阅读MySQL COUNT()和SUM(),以便您理解为什么我在COUNT()所包含的IF()中使用NULL而不是0,以及为什么我只是从中删除了ELSE NULL我在COUNT()中的示例CASE语句。...