比较SQL查询中的两个UNIX时间戳

时间:2015-10-05 11:21:56

标签: php mysql sql unix-timestamp

我希望获取具有hour_status = 1的下一行并且在当前行之后(已经获取并显示)。我希望在UNIX时间戳之后对它们进行排序。我已经在谷歌搜索和搜索尝试不同的东西,但迄今为止没有一个工作。那么如何在SQL查询中执行此操作呢?

想要的结果是: sent_date < (PREVIOUS ROWS sent_date)

这是我已有的代码:

$sql_two = "SELECT FROM_UNIXTIME(sent_date) FROM user_hours WHERE hour_id = ?";
$stmt_two = $mysqli->prepare($sql_two) or die ("Feil i database<br>" . $sql_two . "<br><b>Feilmelding:</b> " . $mysqli->error);
$stmt_two->bind_param('i',$hourId);
$stmt_two->execute() or die("noe gikk galt");
$stmt_two->bind_result($db_prev_sent_date);
$stmt_two->fetch();

$sql = "SELECT hour_id FROM user_hours WHERE hour_status = 1 AND FROM_UNIXTIME(sent_date) < ? ORDER BY FROM_UNIXTIME(sent_date) desc LIMIT 1";
$stmt = $mysqli->prepare($sql) or die ("Feil i database<br>" . $sql . "<br><b>Feilmelding:</b> " . $mysqli->error);
$stmt->bind_param('i',$db_prev_sent_date);
$stmt->execute() or die("noe gikk galt");
$stmt->bind_result($db_hour_id_next);
$stmt->fetch();

1 个答案:

答案 0 :(得分:0)

由于您只需要显示下一行并单独进行更新,因此您的解决方案非常适合您所拥有的场景。