[英]MySQL INNER JOIN with date as condition
我正在嘗試使用內部聯接以日期為條件之一從兩個表中獲取數據。
但是由於第二個表包含dateTime列類型,所以我只想使用日期來檢查條件。
當我使用Postman運行代碼時,它說我error in SQL syntax at line (DATE)checkInDateTime = $rideDate
有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)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.