我在分配后获得选择标签中的值,它不应该显示在选择标签中

时间:2013-01-19 10:16:59

标签: mysql sql jsp

* 父表 * :车辆

CREATE TABLE `vehicle` (
    `vehicle_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `version_number` INT(10) UNSIGNED NULL DEFAULT '0',
    `vehicle_no` VARCHAR(64) NOT NULL COLLATE 'utf8_bin',
    `vehicle_reg_no` VARCHAR(64) NOT NULL COLLATE 'utf8_bin',
    `vehicle_name` VARCHAR(64) NOT NULL COLLATE 'utf8_bin',
    `total_seat` INT(15) NOT NULL,
    `route_id` INT(10) UNSIGNED NOT NULL,
    `available_seat` INT(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`vehicle_id`)
)

子表: allocate_vehicle

CREATE TABLE `allocate_driver` (
    `allocate_driver_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `version_number` INT(10) UNSIGNED NULL DEFAULT '0',
    `vehicle_id` INT(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`allocate_driver_id`),
    INDEX `fk_vehicle_idx` (`vehicle_id`),
     CONSTRAINT `fk_vehicle_idx` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicle` (`vehicle_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,

)

所以我给出了这个查询:select veh.* from vehicle as veh JOIN allocate_driver as allDr on veh.vehicle_id!=allDr.vehicle_id

但不能正常工作......在插入之前我想要分配每辆车后的车辆列表它不应该在列表中显示..任何人都可以告诉我...在此先感谢:)

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您想要找到未分配给any司机的车辆。您的查询将找到未分配给some驱动程序的车辆(并将为每个驱动程序返回一次)。

然后这个查询应该给你你想要的东西:

SELECT veh.* FROM vehicle AS veh
LEFT JOIN allocate_driver as allDr 
ON veh.vehicle_id = allDr.vehicle_id
WHERE allDr.vehicle_id IS NULL