如何查找订单之间的过去天数

时间:2016-04-11 16:27:52

标签: sql oracle11g

如何找到从这组数据中订购的每个菜单项之间经过的天数:

account    menuItem       beginDate      endDate
'123'      'I1'           '2016-01-10'   '2016-01-16'
'145'      'I1'           '2016-03-11'   '2016-03-26'
'156'      'I2'           '2016-02-10'   '2016-02-26'

到目前为止我有这个问题:

Select  menuItem, Datediff(day, beginDay, endDay) DaysOrdered
From MealCheckout
Group By menuItem
Order By beginDate, endDate

所以输出应该是例如menuItem I1有两条记录。因此,应该将第一条记录的开始日期'2016-01-10'和最后一次出现的I1记录的结束日期作为第二条记录并从最后一条记录中取出endDate记录为'2016-03-26'并减去startDateendDate以查找已用时间。上面的查询不起作用,它不输出任何值。

2 个答案:

答案 0 :(得分:1)

您可以在Oracle中减去日期以获取已过去的天数。所以:

Add

您的查询应该在Oracle中返回错误;没有Select menuItem, max(endDay) - min(beginDay) DaysOrdered From MealCheckout Group By menuItem; 功能。

答案 1 :(得分:1)

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
     border: 1px solid black;
}
</style>
</head>
<body>

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM assessments WHERE pupil_id=288";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>subject</th><th>assessment id</th><th>next assessment id</th><th>last assessment id</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row['subject']. "</td><td>" . $row["assessment_id']"</td><td>" . $row['next_assessment_id']. "</td><td>" . $row['last_assessment_id']. "</td></tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

$conn->close();
?> 

</body>
</html>