gpt4 book ai didi

mysql - 在 MySql 中,如何将数据插入到引用其他三个表的表中,然后选择该数据?

转载 作者:行者123 更新时间:2023-11-29 23:45:55 26 4
gpt4 key购买 nike

我有一个appointment 表,它引用了其他三个表:counselorclientroom。我需要将这三个表中的数据插入到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com