MySQL INNER JOIN 以日期为条件
MySQL INNER JOIN with date as condition
我正在尝试使用内部联接从两个 table 中获取数据,使用日期作为条件之一。
但是由于第二个 table 包含 dateTime 列类型,我只想使用日期来检查条件。
当我使用 Postman 运行代码时,它说我有 error in SQL syntax at line (DATE)checkInDateTime = $rideDate
.
我还测试了 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 DATE)
。
我正在尝试使用内部联接从两个 table 中获取数据,使用日期作为条件之一。
但是由于第二个 table 包含 dateTime 列类型,我只想使用日期来检查条件。
当我使用 Postman 运行代码时,它说我有 error in SQL syntax at line (DATE)checkInDateTime = $rideDate
.
我还测试了 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 DATE)
。