简体   繁体   English

MySQL INNER JOIN,日期作为条件

[英]MySQL INNER JOIN with date as condition

I'm trying to use inner join to get the data from two tables using date as one of the condition. 我正在尝试使用内部联接以日期为条件之一从两个表中获取数据。
But since the second table contain dateTime column type, I want to use only the date to check for condition. 但是由于第二个表包含dateTime列类型,所以我只想使用日期来检查条件。
When i run the code using Postman, it says I have error in SQL syntax at line (DATE)checkInDateTime = $rideDate . 当我使用Postman运行代码时,它说我error in SQL syntax at line (DATE)checkInDateTime = $rideDateerror in SQL syntax at line (DATE)checkInDateTime = $rideDate
I also have test the SQL without taking the date into condition and the SQL works . 我也已经在不考虑日期的情况下测试了SQL,并且SQL可以正常工作。
Is there any ways to use the date as condition in InnerJoin method? 有什么方法可以将日期用作InnerJoin方法中的条件? Please help me . 请帮我 。
Thanks. 谢谢。
P/s : my dateTime column store values such as 2015-01-08 11:18:02 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM