正如 Headers 所示,我想在CASE语句的数据库中使用 strtotime
作为两个日期列 .
UPDATE
这是整个PHP代码 .
<?php
$conn=mysqli_connect("localhost","root","nature526","thebase");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn, "SELECT * FROM thetable ORDER BY curdate, case
when mainissue = 'No Water Flow/Low Pressure' AND ((Previous - Recent) / Recent) * 100 >= 120 then 1
when mainissue = 'Leakage' AND address = 'Torko St. San Jose, Antique' AND ((Previous - Recent) / Recent) * 100 > 110 then 2
when mainissue = 'High Meter Readings' then 3
else 4
end asc");
echo " <center> <table border='1'>
<tr>
<th>Full Name</th>
<th>Address</th>
<th>Account Number</th>
<th>Box Number</th>
<th>Designated date for Repairs</th>
<th>Issue</th>
<th>Additional Information</th>
<th>Date Difference</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['actnum'] . "</td>";
echo "<td>" . $row['boxnum'] . "</td>";
echo "<td>" . $row['reqdate'] . "</td>";
echo "<td>" . $row['mainissue'] . "</td>";
echo "<td>" . $row['textarea'] . "</td>";
echo "<td>" . (strtotime($row['curdate']) - (strtotime($row['recentdate']))) / 86400 . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
所以,上面的代码可以工作,但我想包括两个日期列 recentdate
和 curdate
. 但是它让我觉得我应该在查询中使用 strtotime
. 它用于我用它来回显行时 . echo "<td>" . (strtotime($row['curdate']) - (strtotime($row['recentdate']))) / 86400 . "</td>";
. 此公式适用于输出日期列之间的差异 . December 10, 2018
和 December 12, 2018
将在表 2
中输出 . 现在, strtotime
如何在 CASE
语句中工作,以便我可以优先考虑最高的差异?
它理论上应该是这样的:
$result = mysqli_query($conn, "SELECT * FROM thetable ORDER BY curdate, case
when mainissue = 'No Water Flow/Low Pressure' AND ((Previous - Recent) / Recent) * 100 >= 130 AND strotime(curdate - recentdate) / 86400 >= 1 then 1
when mainissue = 'No Water Flow/Low Pressure' AND ((Previous - Recent) / Recent) * 100 >= 120 then 2
when mainissue = 'Leakage' AND address = 'Torko St. San Jose, Antique' AND ((Previous - Recent) / Recent) * 100 > 110 then 3
when mainissue = 'High Meter Readings' then 4
else 5
end asc");
UPDATE
放置一个 DATEDIFF()
而不是_15111也不会起作用,因为它给了我 mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given 错误