带有多个参数的SQL IN子句

时间:2017-09-21 11:25:57

标签: sql oracle

我有一张如下表格

CREATE TABLE student_marks (Name, Subject, Marks ) AS
  SELECT 'Mike', 'Physics',   20 FROM DUAL UNION ALL
  SELECT 'Mike', 'Bio',       20 FROM DUAL UNION ALL
  SELECT 'Mike', 'Chemistry', 30 FROM DUAL UNION ALL
  SELECT 'Ryan', 'Physics',   24 FROM DUAL UNION ALL
  SELECT 'Ryan', 'Bio',       24 FROM DUAL UNION ALL
  SELECT 'Ryan', 'Chemistry', 24 FROM DUAL;

我想得到Mike的物理学和Ryan的所有科目的名称和标记。我想要使​​用的查询是这样的

select *
from   student_marks
where  (Name,Subject) in (('Mike','Physics'),('Ryan',*));

我应该在查询中替换*以便它匹配Ryan的所有主题?

1 个答案:

答案 0 :(得分:3)

*替换为Subject

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE student_marks (Name, Subject, Marks ) AS
  SELECT 'Mike', 'Physics',   20 FROM DUAL UNION ALL
  SELECT 'Mike', 'Bio',       20 FROM DUAL UNION ALL
  SELECT 'Mike', 'Chemistry', 30 FROM DUAL UNION ALL
  SELECT 'Ryan', 'Physics',   24 FROM DUAL UNION ALL
  SELECT 'Ryan', 'Bio',       24 FROM DUAL UNION ALL
  SELECT 'Ryan', 'Chemistry', 24 FROM DUAL;

查询1

select *
from   student_marks
where (Name,Subject) in (('Mike','Physics'),('Ryan',Subject))

<强> Results

| NAME |   SUBJECT | MARKS |
|------|-----------|-------|
| Mike |   Physics |    20 |
| Ryan |   Physics |    24 |
| Ryan |       Bio |    24 |
| Ryan | Chemistry |    24 |