我有一個appointment
表引用三個其他表:counselor
,client
和room
。我需要將這三個表中的數據插入到appointment
表中。我創建了appointment
表如下:在MySql中,如何將數據插入引用其他三個表的表中,然後選擇該數據?
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');
是有沒有辦法做到這一點,我不會硬編碼的字符串名稱插入,並從表中引用它們?有很多值我想插入appointment
。一旦我插入,我將如何選擇要顯示的數據?我是否應該在插入聲明中選擇並加入所有內容?
您的INSERT語句甚至不是有效的語法。 「insert into appointment」後面的列表應該是該表中列的列表,從其他表中指向字段是沒有意義的。 – Barmar 2014-09-20 18:18:09
您需要創建一個sql腳本,它將插入數據並使用insert id生成最後一條INSERT語句 – user4035 2014-09-20 18:21:13