我有三张桌子--主题,学生,注册
学生桌
idstudent // student_name
/T16346.3-1997商业技术和技术技术产品
454
579 -商品
主题表
idsubject /C/ subject_name
401
301 -准准性-商品价格-比较准的准价格
招生表
idsubject (美国)-更多的-更多的
401
301
如何将Max Goodwin的数学课注册到第二季度的招生表中。但只指定名称(例如:“Max Goodwin”,“Math”),而不是SQL语句中的数字(例如idstudent,idsubject)?
发布于 2020-04-02 19:32:53
您可以执行插入-选择:
INSERT INTO enrollment(sdsubject, idstudent, Quarter, final_grade)
SELECT
sub.idsubject,
stu.idstudent
'2Q',
null
FROM
student stu
CROSS JOIN
subject sub
WHERE
stu.student_name = 'Max Goodwin' AND
sub.subject_name = 'Math'但这也许不是完全明智的,因为名称(特别是)不能保证像id是唯一的(在世界上有很多麦克斯古德温)-当然,除非你想把它们全部注册!
发布于 2020-04-02 19:48:24
您可以使用返回ids的子查询:
INSERT INTO ENROLLMENT (idsubject, idstudent, Quarter) VALUES
(
(SELECT idsubject FROM (SELECT idsubject FROM SUBJECT WHERE subject_name = 'Math') t),
(SELECT idstudent FROM (SELECT idstudent FROM STUDENT WHERE student_name = 'Max Goodwin') t),
'2Q'
);见演示。
https://stackoverflow.com/questions/60999562
复制相似问题