这里我正在做出租车分配模块,在这里我将解释我的要求,管理员按类次将出租车分配给员工。这个详细信息我存储在 cab_allocation 分配表中。
cab_allocation (Table Name)
allocationId date cabId shiftTiming
1 2017-08-12 CBX100 1
2 2017-08-12 CBX100 2
3 2017-08-12 CBX101 3
此处 cabId CBX100 每天有两次分配,CBX101 每天有一次分配。之后我想跟踪这次旅行是否发生,对于我存储在 trip_details 表中的跟踪详细信息
trip_details (table name)
tripId cabNo shiftId tripDate startTime endTime tripStatus
1 CBX100 1 2017-08-12 09:30:00 11:30:00 1
2 CBX100 2 2017-08-12 12.10.00 0
此处 cabId CBX100 已完成其 shiftId 1,shiftId 开始时间为 09:30:00,结束时间为 11:30:00,tripStatus 1 表示此行程已完成。
下一辆 cabId CBX100 已开始二类,开始时间为 12.10.00 ,但此行程尚未完成,因此没有结束时间和行程状态
下一个cabId CBX101还没有开始他的行程,所以没有条目是这个表,
现在我的预期结果是旅程不完整,我想了解出租车分配的详细信息。这里 cab_allocation 表 AllocationId 我正在使用 forien key trip_details 表 tripId。
I tried but i am not getting exact result,because CBX101 cabId not yet started his trip ,so entry not there in trip_details table, so i am not able get details.
我尝试过这样
$sql = "SELECT * FROM `cab_allocation` a INNER JOIN trip_details b ON a.allocationId=b.tripId WHERE b.tripStatus !='1'";
$mysql = mysql_query($sql);
$count =mysql_num_rows($mysql);
if($count > 0){
while ($row = mysql_fetch_assoc($mysql)) {
$data[] = $row;
}
$arrayName = array('status' => 'success', 'count' => $count, 'data' =>$data );
echo json_encode($arrayName);
}else{
$arrayName = array('status' => 'error', 'data' =>'Data not found' );
echo json_encode($arrayName);
}
我正在获取结果
{
"status": "success",
"count": 1,
"data": [
{
"allocationId": "2",
"date": "2017-08-12",
"shiftTiming": "2",
"routeId": "1",
"cabId": "CBX100",
"tripId": "2",
"cabNo": "CBX100",
"driverId": "G2E100",
"shiftId": "2",
"tripDate": "",
"startTime": "12.10.0",
"endTime": "",
"tripStatus": "0"
}
]
}
预期结果
{
"status": "success",
"count": 2,
"data": [
{
"allocationId": "2",
"date": "2017-08-12",
"shiftTiming": "2",
"cabId": "CBX100",
},
{
"allocationId": "3",
"date": "2017-08-12",
"shiftTiming": "3",
"cabId": "CBX101",
},
]
}
最佳答案
正确的 SQL 是:
SELECT a.allocationId, a.date, a.shiftTiming, a.cabId
FROM cab_allocation a
LEFT JOIN trip_details b
ON a.allocationId = b.tripId
WHERE b.tripStatus !='1'
LEFT JOIN 将匹配 cab_allocation 上不在 trip_details 上的记录
关于php - 连接两个表时联接查询无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45643922/