简体   繁体   English

MySQL:如何以多对多关系在两个表之间创建映射

[英]MySQL: How to create a mapping between two tables in a many-to-many relationship

I have two relevant tables: 我有两个相关的表:

Trainee and Device 学员和设备

Each trainee needs to do a training with the device, in order to be allowed to use it. 为了使每个学员都可以使用该设备,需要对其进行培训。 As a trainee needs to be trained in multiple devices and one single device can be used by multiple users I have created a many-to-many relationship between those two. 由于受训人员需要使用多种设备进行培训,并且一个设备可以供多个用户使用,因此我在这两个设备之间建立了多对多关系。

MySQLWorkbench therefore created me another table which is called 'trainee_has_device' and holds a foreign key to trainee.Id and a foreign key to device.Id. 因此,MySQLWorkbench为我创建了另一个名为“ trainee_has_device”的表,该表包含Trainee.Id的外键和device.Id的外键。

So far, so good. 到现在为止还挺好。

I need to get the training status for a trainee to a specific device, so I added a property TrainingStatus on 'trainee_has_device'. 我需要获取针对特定设备的受训者的培训状态,因此我在'trainee_has_device'上添加了属性TrainingStatus。 My tables then look like this: 我的表如下所示:

|trainee|             
--------------------- 
id 
surname

|device|
---------------------
id
deviceName

|trainee_has_device|
---------------------
traineeId
deviceId
TrainingStatus

I need to get the training value of every person to every device even if there is no entry in 'trainee_has_device' (if so I would like to get null) 即使在'trainee_has_device'中没有条目,我也需要获取每个人对每个设备的培训价值(如果这样,我想获取空值)

My query looked like this: 我的查询如下所示:

SELECT * FROM trainee LEFT OUTER JOIN trainee_has_device ON trainee_has_device.traineeId = trainee.id LEFT OUTER JOIN device ON device.id = trainee_has_device.deviceId

But Unfortunately I did only get entries which were listed in trainee_has_device or null. 但是不幸的是我只得到了列在trainee_has_device或null中的条目。

How can I get the TrainingStatus of every person to every device even if there is no entry in 'trainee_has_device'? 即使'trainee_has_device'中没有任何条目,如何获得每个人到每个设备的TrainingStatus?

I need to get the training value of every person to every device even if there is no entry in 'trainee_has_device' 即使在'trainee_has_device'中没有条目,我也需要获取每个人对每个设备的培训价值

This doesn't make sense, the trainee_has_device table has to be filled first or it won't show you any data. 这没有任何意义,trainee_has_device表必须首先填充,否则它不会显示任何数据。 In real world, all of the trainees has to be assigned a set of devices with a default status (New,Familiar...) for each assignment. 在现实世界中,必须为所有学员分配一组具有默认状态(新,熟悉...)的设备。 So if the connection isn't available between Trainee A and Device C it just means that Device C isn't assigned to Trainee A 因此,如果学员A和设备C之间的连接不可用,则仅表示设备C未分配给学员A

I managed to get it working with a subquery: 我设法使其与子查询一起工作:

SELECT trainee.id as TraineeId, trainee.Prename, trainee.Surname, trainee.Mail, trainee.Department,
trainee.Telephone, device.Name as DeviceName, (SELECT IF (trainee_has_device.trainee_id = trainee.id AND
trainee_has_device.device_id = device.id, trainee_has_device.TrainingStatus, 'N/A')
FROM trainee_has_device WHERE trainee_has_device.trainee_id = trainee.id AND device.id = trainee_has_device.device_id) AS TrainingStatus
FROM trainee JOIN device ORDER BY trainee.Surname, trainee.id, device.Name ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM