[英]How to write query for this condition
I am doing a project for a travel agency. 我正在为旅行社做一个项目。 i have to joing 3 tables 我要去3张桌子
vehicle_details
which i store all vehicle details vehicle_details
,我存储所有车辆详细信息 rac_details
booked vehicle details rac_details
预定车辆的详细信息 owner
vehicle owners details owner
车主的详细信息 While booking i need to search the vehicles available in that date range in rac_details
. 在预订时,我需要搜索rac_details
在该日期范围内可用的车辆。
$qry=" SELECT `vehicle_details`.*, `owner`.`owner_name`, `owner`.`id` As owner_id,
`rac_details`.`from_date`, `rac_details`.`to_date`,
`rac_details`.`vehicle_id`
FROM (`vehicle_details`)
LEFT JOIN `rac_details` ON `rac_details`.`vehicle_id`=`vehicle_details`.`id`
AND `rac_details`.`from_date` NOT BETWEEN $from_timestamp AND $to_timestamp
AND `rac_details`.`to_date` NOT BETWEEN $from_timestamp AND $to_timestamp
JOIN `owner` ON `owner`.`id`=`vehicle_details`.`owner_id`
WHERE `vehicle_details`.`model_id` = '$model_id' ";
By the above query i am getting all vehicle details. 通过以上查询,我可以获得所有车辆详细信息。 when inner join
rac_details
i am not able to get the newly added vehicle ie which do not have record in rac_details
. 当inner join
rac_details
时,我无法获得新添加的载具,即在rac_details
没有记录的rac_details
。 Hope anyone can help me 希望有人能帮助我
Edit 编辑
For example i got a booking for Polo with reg no XX-YY-ZZ from feb 27th to march 3. then i need to search whether the vehicle xx-yy-zz is available in that days for that i just searched in my rac_details where i store booked vehicle details. 例如,我从2月27日至3月3日预订了编号为XX-YY-ZZ的Polo车,那么我需要搜索那一天是否有车辆xx-yy-zz,因为我只是在rac_details中搜索了我存储预订的车辆详细信息。 Then we bought a new vehicle Vento with reg no RR-TT-YY so i added that vehicle in my vehicle_details table and no records will be added to rac_details before the first booking 然后,我们购买了新车Vento,注册编号为RR-TT-YY,所以我在我的vehicle_details表中添加了该车辆,并且在第一次预订之前不会在rac_details中添加任何记录
if i write this query 如果我写这个查询
$qry=" SELECT `vehicle_details`.*, `owner`.`owner_name`, `owner`.`id` As owner_id, `rac_details`.`from_date`, `rac_details`.`to_date`, `rac_details`.`vehicle_id` FROM (`vehicle_details`) JOIN `rac_details` ON `rac_details`.`vehicle_id`=`vehicle_details`.`id` AND `rac_details`.`from_date` NOT BETWEEN $from_timestamp AND $to_timestamp AND `rac_details`.`to_date` NOT BETWEEN $from_timestamp AND $to_timestamp JOIN `owner` ON `owner`.`id`=`vehicle_details`.`owner_id` WHERE `vehicle_details`.`model_id` = '$model_id' ";
when i book vento for the first time it will not show vento in the list since there is no record in rac_details
for vento 当我第一次预订vento时,它不会在列表中显示vento,因为rac_details
没有关于vento的记录
I would try it with NOT IN
the already reserved vehicles: 我会用NOT IN
在已经预订的车辆中尝试:
"SELECT `vehicle_details`.*, `owner`.`owner_name`, `owner`.`id` As owner_id,
`rac_details`.`from_date`, `rac_details`.`to_date`,
`rac_details`.`vehicle_id`
FROM (`vehicle_details`)
JOIN `owner` ON `owner`.`id`=`vehicle_details`.`owner_id`
WHERE `vehicle_details`.`model_id` = '$model_id'
AND vehicle_details.id NOT IN (
SELECT rac_details.vehicle_id
FROM rac_details
WHERE `rac_details`.`from_date` BETWEEN $from_timestamp AND $to_timestamp
AND `rac_details`.`to_date` BETWEEN $from_timestamp AND $to_timestamp
)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.