如何在某个字段的另一行中选择具有相同值的行(不选择其他行)

时间:2017-03-27 23:03:59

标签: mysql

嗨,我刚刚接触sql,我无法理解工会是如何运作的。它说我需要检索所有其他(不是s001)学生,他们已经学习了与's001'相同的课程,使用了Union或者交叉或减去。我做了一个查询,它没有工作它有错误。我的疑问是:

select student.* from student,subjcode where subjcode.sno!='s001' union select subjcode.* from subjcode,student where subjcode.sno='s001' and subjcode.sno=student.sno;

表subjcode是:

sno     cno     score
S001    C001    78.90
S001    C002    82.90
S001    C003    59.00
S002    C001    80.90
S002    C002    72.90
S003    C001    81.90
S003    C002    81.90
S004    C001    60.90
S005    C002    50.00
S006    C002    50.00

表学生是:

sno     sname   sage ssex
S003    BILL    25   M
S004    STEVE   20   F
S005    BAKR    20   F
S006    TOM     21   M
S007    JERRY   21   M
S008    MACY    21   F
S009    MICK    23   F
S010    COOKER  22   F

请有人帮我解决这个问题。非常感谢!

1 个答案:

答案 0 :(得分:0)

听起来你正在寻找INNER JOIN而不是UNION

这能否为您提供以后的结果?

SELECT student.*, subjcode.cno, subjcode.score
FROM student
INNER JOIN subjcode ON subjcode.sno = student.sno
WHERE student.sno LIKE '%S001%';