我正在为旅行社做一个项目。我必须加入3桌
vehicle_details
rac_details
已预订车辆详情owner
车主详情预订时我需要在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' ";
通过上述查询,我得到了所有车辆的详细信息。当inner join
rac_details
我无法获得新添加的车辆时,即rac_details
中没有记录。
希望有人能帮助我
修改
例如我从27日到3月3日预订了Polo,并且没有XX-YY-ZZ。那么我需要搜索车辆xx-yy-zz在那几天是否可用我的rac_details我存储了预定的车辆详细信息。 然后我们购买了一辆没有RR-TT-YY的新车Vento,所以我在我的vehicle_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`) 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' ";
当我第一次预订vento时,它不会在列表中显示vento,因为rac_details
中没有for vento的记录
答案 0 :(得分:1)
我会尝试使用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
)"