判断车辆是否可以被驱动的最佳方法是什么。
假设我们有不同的驾驶执照类型: A,B,BE(等等)
我有以下表格......
Person ( ID, Name, LicenseTypes )
DrivingLicense ( ID, Type, ExpDate )
LicenseTypes ( ID, Type )
Vehicle ( ID, Brand, VehicleType )
VehicleType ( ID, VehicleType )
我想要完成,如果一个人的牌照类型为“A”并且他拥有的车辆是一辆汽车,他可能不会开车,因为这辆车需要“B”。
如果此人有多种驾驶执照类型,请说:A和B,其中汽车的要求是B,他可以驾驶车辆。
最好在Vehicletype表中添加一个列,使用Licensetype并加入这些表吗?
或者我可以使用MySQL line / PHP命令来比较这些值并使用某种语句吗? IF值=“”和“”,但我认为这将是一个混乱的解决方案。
答案 0 :(得分:0)
我不确定你是否会以你想要的结构开始。我想如果你用自然语言写出来,你可能会有更好的观点:
所以我会选择以下表格:
***Person*** ***DrivingLicense*** ***DrivingLicenseLicenseTypes***
ID ID ID
Name ExpiryDate DrivingLicense_ID
Person_ID LicenseType_ID
***LicenseTypes*** ***LicenseTypeVehicleTypes*** ***VehicleTypes*** ***Vehicles***
ID ID ID ID
LicenseType LicenseType_ID VehicleType Make
VehicleType_ID Model
VehicleType_ID
现在,我可以拥有一辆名为VehicleType A的摩托车和一辆驾驶执照,它是LicenseType B,但是它允许我同时驾驶车型A和B,所以我知道我有驾驶我的摩托车的许可。 / p>
ETA:啊,你是对的,我需要LicenseTypes和VehicleTypes之间的另一个链接表LTVT。现在,您可以在单一许可证类型上拥有多种车辆类型。因此,您可以拥有LicenseType B1(汽车+拖车),其中包括VehicleType B和B1(汽车和汽车和拖车)。这回答你的目的吗?
EDIT2:
让我们在这里贴一些样本数据。
Person: (1, 'goodevans'), (2, 'John');
DrivingLicense: (1, '2015-01-01', 1), (2, '2015-10-01', 2);
VehicleTypes: (1, 'Car'), (2, 'Motorbike');
LicenseTypes: (1, 'Car'), (2, 'Motorbike');
Vehicles: (1, 'Ford', 'Focus', 1), (2, 'Chevrolet', 'Caprice', 1), (3, 'Ducati', 'Multistrada', 2);
LicenseTypeVehicleTypes: (1, 1, 1), (1, 1, 2), (1, 2, 2);
DrivingLicenseLicenseTypes: (1, 1, 1), (1, 2, 2);
好的,鉴于以上数据:
LTVT的前两行显示LicenceType 1(Car)适用于VehicleType 1(Car)和2(Bike)
第三排LVTV显示LicenseType 2(自行车)仅适用于VehicleType 2(自行车)
DLLT的第一行显示我的许可证ID 1是Car类型。
第二行DLLT显示您的许可证ID 2是摩托车类型。
我有一个车型许可证,有效期至明年1月,这使我可以驾驶汽车和 摩托车。
你有摩托车驾照,有效期至明年10月,只允许你骑摩托车。
这清楚了吗?
答案 1 :(得分:0)
好的,这将是漫长的。我希望你能得到我在这里尝试过的一般要点;总而言之,我使用了6张桌子。此外,我没有使用你的表名(在我试用我的东西时,在问题和评论中有一些编辑),抱歉'回合那个。
以下代码适用于例如sqlite3
我认为我只使用ANSI SQL,所以它应该适用于所有内容。使用您当地数据库的特殊性来改进;例如,我没有输入自动增量ID,只使用varchar
作为文本,因为这是最大的共同点。
在每个表定义下面,我放入一些虚拟dataload语句来查看是否一切正常,并且我输入了一些select
语句来制作一个很好的列表。当您在sqlite3
中运行时,您会得到以下输出:
What person has which license?
Karel A required to drive a motorcycle
Karel B required to drive a car
John AM required to drive a scooter
John B required to drive a car
What vehicle types may be driven with which licenses?
car B
scooter AM
scooter A
scooter B
motorcycle A
Which person may drive which vehicle given their license?
Karel Solex scooter A required to drive a motorcycle
Karel Vespa scooter A required to drive a motorcycle
Karel BMW K1200GT motorcycle A required to drive a motorcycle
Karel Ducati Monst motorcycle A required to drive a motorcycle
Karel Peugeot 307 car B required to drive a car
Karel Volvo V70 car B required to drive a car
Karel Solex scooter B required to drive a car
Karel Vespa scooter B required to drive a car
John Solex scooter AM required to drive a scooter
John Vespa scooter AM required to drive a scooter
John Peugeot 307 car B required to drive a car
John Volvo V70 car B required to drive a car
John Solex scooter B required to drive a car
John Vespa scooter B required to drive a car
转入SQL代码。首先,您拥有他们持有的人员和许可证。我使用典型的交叉表对此进行了建模; person
包含有关人员的信息,license
有关许可证类型的信息,person_license
是表示哪个人拥有哪些许可证的交叉表:
-- Known persons
create table person (
person_id integer not null unique primary key,
person_name varchar(255) not null
);
insert into person (person_id, person_name) values (1, 'Karel');
insert into person (person_id, person_name) values (2, 'John');
-- Known license types
create table license (
license_id integer not null unique primary key,
license_name varchar(3) not null unique,
license_desc varchar(255)
);
insert into license (license_id, license_name, license_desc)
values (1, 'AM', 'required to drive a scooter');
insert into license (license_id, license_name, license_desc)
values (2, 'A' , 'required to drive a motorcycle');
insert into license (license_id, license_name, license_desc)
values (3, 'B' , 'required to drive a car');
-- What licenses do the persons hold (cross table so that 1 person may hold
-- multiple licenses. For example: Karel has A and B, John has AM and B.
-- NOTE: There is no expiry date here, you could stick it into this table when
-- you need it.
create table person_license (
person_id integer references person(person_id) not null,
license_id integer references license(license_id) not null
);
insert into person_license (person_id, license_id) values (1, 2);
insert into person_license (person_id, license_id) values (1, 3);
insert into person_license (person_id, license_id) values (2, 1);
insert into person_license (person_id, license_id) values (2, 3);
-- Here is a listing to show what licenses a given person has.
select 'What person has which license?';
select person.person_name, license.license_name, license.license_desc
from person, license, person_license
where person_license.person_id = person.person_id
and person_license.license_id = license.license_id;
select '';
接下来是系统中的车辆和车辆类型。对于演示,我有三种类型;滑板车,摩托车和普通汽车。根据您需要支持的许可证类型,您可能希望将其更改为无挂车和汽车挂车等。表vehicle_type
当然是类型,vehicle
是系统中所有车辆的表。它具有1:N关系,因此每辆车列出了类型。
-- Vehicle types.
create table vehicle_type (
vehicle_type_id integer not null unique primary key,
vehicle_type_name varchar(255) not null
);
insert into vehicle_type (vehicle_type_id, vehicle_type_name)
values (1, 'car');
insert into vehicle_type (vehicle_type_id, vehicle_type_name)
values (2, 'scooter');
insert into vehicle_type (vehicle_type_id, vehicle_type_name)
values (3, 'motorcycle');
-- Known vehicles in our system (2 cars, 2 scooters, 2 motorbikes)
create table vehicle (
vehicle_id integer not null unique primary key,
vehicle_name varchar(255) not null,
vehicle_type_id integer not null references vehicle_type(vehicle_type_id));
insert into vehicle (vehicle_id, vehicle_name, vehicle_type_id)
values (1, 'Peugeot 307', 1);
insert into vehicle (vehicle_id, vehicle_name, vehicle_type_id)
values (2, 'Volvo V70', 1);
insert into vehicle (vehicle_id, vehicle_name, vehicle_type_id)
values (3, 'Vespa', 2);
insert into vehicle (vehicle_id, vehicle_name, vehicle_type_id)
values (4, 'Solex', 2);
insert into vehicle (vehicle_id, vehicle_name, vehicle_type_id)
values (5, 'BMW K1200GT', 3);
insert into vehicle (vehicle_id, vehicle_name, vehicle_type_id)
values (6, 'Ducati Monster', 3);
然后是车辆类型和所需许可证之间的关系。我将其设为N:N关系,这样一个许可证允许您驾驶多种车型。在这个演示中,汽车许可证允许您驾驶汽车或小型摩托车;摩托车许可证允许您驾驶摩托车或踏板车,而踏板车许可证只允许您驾驶摩托车,但没有别的。
-- What license do you need for what type of a vehicle?
-- A scooter license allows you only to drive a scooter.
-- A motorcycle license allows you to drive a motorbike and a scooter.
-- A car license allows you to drive a car and a scooter.
create table vehicle_type_license (
vehicle_type_id integer not null references vehicle_type(vehicle_type_id),
license_id integer not null references license(license_id)
);
-- Car requires B
insert into vehicle_type_license (vehicle_type_id, license_id) values (1, 3);
-- Scoorter requires AM, A or B
insert into vehicle_type_license (vehicle_type_id, license_id) values (2, 1);
insert into vehicle_type_license (vehicle_type_id, license_id) values (2, 2);
insert into vehicle_type_license (vehicle_type_id, license_id) values (2, 3);
-- Motorcycle requires A
insert into vehicle_type_license (vehicle_type_id, license_id) values (3, 2);
-- And a listing to show it
select 'What vehicle types may be driven with which licenses?';
select vehicle_type.vehicle_type_name, license.license_name
from vehicle_type, license, vehicle_type_license
where vehicle_type.vehicle_type_id = vehicle_type_license.vehicle_type_id
and license.license_id = vehicle_type_license.license_id;
select '';
最后......这是谁可能驾驶什么车辆的结果:
-- Finally...
-- Let us see what vehicles the persons may drive.
select 'Which person may drive which vehicle given their license?';
select person.person_name, vehicle.vehicle_name,
vehicle_type.vehicle_type_name,
license.license_name, license.license_desc
from person, vehicle, vehicle_type,
license, person_license, vehicle_type_license
where vehicle.vehicle_type_id = vehicle_type.vehicle_type_id
and license.license_id = person_license.license_id
and person.person_id = person_license.person_id
and vehicle_type_license.vehicle_type_id = vehicle_type.vehicle_type_id
and vehicle_type_license.license_id = person_license.license_id
;