【发布时间】:2015-01-08 04:08:06
【问题描述】:
我正在尝试使用内连接从两个表中获取数据,使用日期作为条件之一。
但是由于第二个表包含 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
-
1) 你真的应该使用 mysqli 或 pdo (mysql 已弃用) 2) $rideDate 是什么样子的(请举例)?
-
我猜
(DATE)checkInDateTime是一种类型转换的尝试,而您会收到 sql 错误,因为这不是正确的语法......请尝试使用CAST(checkInDateTime AS DATE)。 -
@JoelCox 绝对正确。更好的问题是您为什么要按日期时间命名列...?
-
@Joe T : $rideDate = 2015-01-08