MySQL INNER JOIN以日期为条件

时间:2015-01-08 04:08:07

标签: php mysql

我正在尝试使用内连接从两个表中获取数据,使用日期作为条件之一。
但是由于第二个表包含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);
}

1 个答案:

答案 0 :(得分:1)

我猜测(DATE)checkInDateTime是一次类型转换的尝试,而且你得到了sql错误,因为这不是正确的语法...试试CAST(checkInDateTime AS DATE)代替。