我正在开发公交车站管理系统。我正在尝试在时间表中排队公交车。 所以我想要的是这样的:
我想显示特定车站不在特定时刻表排队的所有公交车。
- 就像一个车站有 5 辆公交车,那么其中两辆在特定时间表的队列中,我想显示要选择到队列中的所有其他 3 辆公交车。
公交车表
车站表
所以我就这样做了
public function getBusQueue(Request $request)
{
$id = $request->id; // id of the targert schedule
// getting the target station id;
$schedule = Schedule::find($id);
$stations_id = $schedule->station_id;
// getting the buses that are not in queues table
$buses = DB::table("buses")->select('*')
->whereNotIn('bus_number',function($query) {
$query->select('bus_number')->from('queues');
})
->where('Schedule_id', $id)
->where('station_id', $stations_id)
->get();
$data = $buses;
return Response()->json($data);
}
最佳答案
如果我理解正确的话,这个查询中的相关时间表是队列的,而不是公交车的。
(此外,您似乎输错了 buses
表中的 schedule_id
列名称。它当前名为 schedual_id
。)
尝试将查询更改为以下内容:
$buses = DB::table("buses")->select('*')
->whereNotIn('bus_number',function($query) use($id) {
$query->select('bus_number')
->from('queues')
->where('schedule_id', $id);
})
->where('station_id', $stations_id)
->get();
关于mysql - Laravel:根据另一个表中的其他数据选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56516942/