我正在尝试使用内部联接来使用日期作为条件之一从两个表中获取数据。
但是由于第二个表包含 dateTime 列类型,我只想使用日期来检查
当我使用 Postman 运行代码时,它说我在 (DATE)checkInDateTime = $rideDate 行的 SQL 语法中有 错误
。
我还测试了 SQL,但没有将日期纳入条件,并且 SQL 有效。
有什么方法可以在 InnerJoin 方法中使用日期作为条件吗?请帮我 。
谢谢。
P/s:我的 dateTime 列存储值,例如 2015-01-08 11:18:02
//get all rides from table rides
$result = mysql_query("SELECT first.ID, first.fullname,
second.checkInDateTime FROM first INNER JOIN second ON first.ID =
second.riderID WHERE second.ridesID = $rideID AND second.
CAST(checkInDateTime AS DATE) = $rideDate") or die(mysql_error());
//check for empty result
if(mysql_num_rows($result) > 0) {
//loop all result and put into array riders
$response["riders"] = array();
while ($row = mysql_fetch_array($result)) {
//temp array
$rider = array();
$rider["riderID"] = $row["ID"];
$rider["riderName"] = $row["fullname"];
$rider["timeCheckedIn"] = $row["checkInDateTime"];
//push single ride into final response array
array_push($response["riders"], $rider);
}
//success
$response["success"] = 1;
//print JSON response
echo json_encode($response);
} else {
//no rides found
$response["success"] = 0;
$response["message"] = "No riders found";
//print JSON response
echo json_encode($response);
}
最佳答案
我猜测 (DATE)checkInDateTime
是一种类型转换的尝试,并且您收到 sql 错误,因为这不是正确的语法...尝试 CAST(checkInDateTime AS日期)
相反。
关于php - MySQL INNER JOIN 以日期为条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832767/