如何从其他表中获取数据?

时间:2015-02-03 10:07:52

标签: mysql sql

列出注册“CS-204”课程和“CS-102”课程

的学生姓名
table1:enroll
student id , code, grade
table2:student
student id ,name ,bdate,address

..................................  我试着

select student.name,enrol.code from student,enrol where code='CS-204' or code='CS-102'

5 个答案:

答案 0 :(得分:0)

你必须加入两个表:

select student.name,enrol.code 
from student join enrol on student.student_id=enrol.student_id 
where code='CS-204' and student.student_id in (
   select student.name,enrol.code 
   from student join enrol on student.student_id=enrol.student_id  
   where code='CS-102'
)

答案 1 :(得分:0)

您需要加入表格

SELECT s.name, e.code
FROM student s
INNER JOIN enroll e ON s.student_id = e.student_id
WHERE e.code='CS-204' OR e.code='CS-102'

答案 2 :(得分:0)

您需要在WHERE子句中再添加一个参数:

SELECT student.name, enroll.code 
FROM student, enroll 
WHERE (student.student_id=enroll.student_id) AND (enroll.code='CS-204' OR enroll.code='CS-102')

答案 3 :(得分:0)

您可以尝试这样做。

Select student.name from student join enroll
on student.student_id = enroll.student_id where 
enroll.code in ('CS-204','CS-102');

答案 4 :(得分:0)

您也可以通过这种方式获得两门课程的学生姓名:

SELECT b.name as student_name 
FROM enroll a, student b 
WHERE (a.student_id=b.student_id) AND (a.code='CS-204' or a.code='CS-102'`)