0

*父表* : 车辆

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

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

4

1 回答 1

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
于 2013-01-19T11:34:40.057 回答