如果元组是我正在讨论的术语,我不是100%,但我正在看这样的事情:
Table grades
user grade
------------
Jim B
Bill C
Tim A
Jim B+
我知道我能做到:
SELECT COUNT(*)
FROM grades
WHERE (
(user = 'Jim' AND grade = 'B')
OR (user = 'Tim' AND grade = 'C')
);
但有没有办法做更像这样的事情?
SELECT COUNT(*)
FROM grades
WHERE (user, grade) IN (('Jim','B'), ('Tim','C'));
编辑:作为旁注,我只测试过:
(user, grade) = ('Tim','C')
那就失败了,所以我认为IN也会失败,但我错了(谢天谢地!)。
答案 0 :(得分:17)
您发布的查询应该是有效的语法
SQL> ed
Wrote file afiedt.buf
1 with grades as (
2 select 'Jim' usr, 'B' grade from dual
3 union all
4 select 'Bill', 'C' from dual
5 union all
6 select 'Tim', 'A' from dual
7 union all
8 select 'Jim', 'B+' from dual
9 )
10 select *
11 from grades
12 where (usr,grade) in (('Jim','B'),
13 ('Tim','C'),
14* ('Tim','A'))
SQL> /
USR GR
---- --
Jim B
Tim A
答案 1 :(得分:0)
您可以使用子查询来处理像表一样的元组列表:
SELECT COUNT(*)
FROM grades
JOIN (
SELECT 'Jim' as user, 'B' as grade from dual
UNION ALL
SELECT 'Tim', 'C' from dual
UNION ALL
SELECT 'Pim', 'D' from dual
) as SearchTarget
ON SearchTarget.user = grades.user
and SearchTarget.grade = grades.grade