如何插入带有条件的SQL语句

时间:2018-10-31 04:34:28

标签: sql postgresql

我有3张桌子

学生:

studentid | fname | lname | add | dob
---------------------------------------------------------
 01       | A     | a     | x   | 01/01/1997
 02       | B     | b     | y   | 02/02/1995
 03       | C     | c     | z   | 03/03/1993

课程:

courseid | name | prerequisites
-------------------------------
IPG      | ipg  |  NULL 
PPL      | ppl  | IPG
SEG      | seg  | PPL

报名:

student | course | semester | finalgrade
-----------------------------------------
01      | IPG    | 1        | G
02      | PPL    | 2        | E
03      | PPL    | 2        | F

如何通过验证课程表中的先决条件,如何在课程PPL中注册学生01?

学生必须先注册IPG课程,然后才能注册PPL课程。

1 个答案:

答案 0 :(得分:2)

insert into enrolment (student, course) 
            select 01,'IPG' from enrolment where student=01 
            and course = (select prerequisites from course where courseid ='IPG');

您可以尝试组合还是插入并选择。