【问题标题】:Join query not working properly while connecting two tables连接两个表时连接查询无法正常工作
【发布时间】:2017-08-11 21:18:25
【问题描述】:

我在这里做出租车分配模块,在这里我将解释我的要求,管理员按班次将出租车分配给员工。这个细节我存储在 cab_allocation 分配表中。

cab_allocation(表名)

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(表名)

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 table allocationId 我正在使用 forien key trip_details table tripId。

我试过了,但我没有得到确切的结果,因为 CBX101 cabId 还没有开始他的旅行,所以trip_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 的情况下提供帮助?
  • 哦,对不起,我会上传我的代码
  • Mr@ Felippe Duarte,请查看我更新的代码,我上传了我尝试过的内容

标签: php mysql php-5.6


【解决方案1】:

正确的 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 上的记录

【讨论】:

  • Mr@Felippe Duarte,我使用了你的代码,但我得到了 { "status": "success", "count": 1, "data": [ { "allocationId": "2", “日期”:“2017-08-12”,“shiftTiming”:“2”,“cabId”:“CBX100”}]},
  • { "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", }, ] }
  • 我分享了我的预期答案,请检查
【解决方案2】:

用户 LEFT JOIN 而不是 INNER JOIN

SELECT a.* FROM cab_allocation as a 
LEFT JOIN 
trip_details as b ON a.allocationId=b.tripId 
WHERE b.tripStatus !='1'

这张图一定能帮到你

【讨论】:

  • 我使用了 LEFT Join 但我得到了精确的分析器,我只得到一个值,cabId CBX101 还没有开始我想要的,但我没有得到
  • 对不起,我没有得到预期的答案,请检查并更新我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多