假设我在sql中有以下表格。
student
name id
alex 1
felix 2
hannah 3
john 4
jonas 5
instructor
name id
alex 1
felix 2
hannah 3
john 4
jonas 5
(ex)brad 8
tom 9
tonny 11
instructor_lectures
name id course_count
alex 1 5
felix 2 2
hannah 3 0
john 4 23
jonas 5 12
(ex)brad 8 1
tom 9 11
tonny 11 9
我想做的是;
- firstly, get the difference of instructor student tables according to the name which does not start with "(ex)"
- and then list course_count of the result.
我的意思是,先得到
name id
tom 9
tonny 11
然后
name id course_count
tom 9 11
tonny 11 9
到目前为止我做了什么?
我已经编写了差异查询并从instructor_lectures中选择查询,但无法将它们组合在一起。
SELECT NAME FROM instructor INSTR WHERE NOT EXISTS
(
SELECT * FROM student STU WHER INSTR.NAME = STU.NAME
)
AND INSTR.NAME NOT REGEXP '^(ex)'
此查询返回
name id
tom 9
tonny 11
现在我想通过查看table instructor_lectures
来获取course_countSELECT NAME FROM instructor INSTR WHERE NOT EXISTS
(
SELECT * FROM student STU WHER INSTR.NAME = STU.NAME
)
AND INSTR.NAME NOT REGEXP '^(ex)'
SELECT * FROM instructor_lectures INSTR_LECTURES WHERE INSTR.NAME = INSTR_LECTURES.NAME
但这会引发错误。我的错误在哪里?如何获取第一个差异查询结果的course_count?
答案 0 :(得分:1)
SELECT INSTR.NAME, INSTR.id, INSTR_LECTURES.COURSE_COUNT
FROM instructor INSTR, instructor_lectures INSTR_LECTURES
WHERE NOT EXISTS (SELECT * FROM student STU WHER INSTR.NAME = STU.NAME)
AND INSTR.NAME NOT REGEXP '^(ex)'
and INSTR.NAME = INSTR_LECTURES.NAME
使用加入条件,了解joins
上的信息