我有一个appointment
表,它引用了其他三个表:counselor
、client
和room
。我需要将这三个表中的数据插入到 appointment
表中。我创建了约会
表,如下所示:
create table appointment(
AppointmentID int default next value for NDFCID primary key,
CounselorID int not null references counselor,
ClientID int not null references client,
RoomNumber int not null references room,
AppointmentDate date,
StartTime time(0),
Duration varchar(50)
);
将数据插入其他三个表后,我想将数据插入约会,这就是我到目前为止所拥有的:
insert into appointment(counselor.firstName, client.firstName, room.RoomName, AppointmentDate,StartTime,Duration)
values
('Audrey', 'Sarah', 'Clear sky', '10/1/2014', '8:00:00', '90');
有什么方法可以做到这一点,我不必将字符串名称硬编码到插入中,而只需从表中引用它们?我想将许多值插入到约会
中。插入后,我将如何选择要显示的数据?我应该在插入语句中选择并加入所有内容,还是会在之后进行?
最佳答案
您使用 SELECT
语句从具有特定名称的其他表中获取 ID。
INSERT INTO appointment (CounselorID, ClientID, RoomNumber, AppointmentDate, StartTime, Duration)
SELECT c.id, cl.id, r.roomNumber, "10/1/2014", "8:00:00", "90"
FROM counselor AS c
CROSS JOIN client AS cl
CROSS JOIN room AS r
WHERE c.firstName = "Audrey"
AND cl.firstName = "Sarah"
AND r.roomName = "Clear sky"
关于mysql - 在 MySql 中,如何将数据插入到引用其他三个表的表中,然后选择该数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951880/