开发者

Is it possible to compare tuples in oracle-compatible sql?

I'm not 100% if tuples is the term for what I'm talking about but I'm looking at something like this:

Table grades
user    grade
------------
Jim     B
Bill    C
Tim     A
Jim     B+

I know I can do:

SELECT COUNT(*)
FROM grades
WHERE开发者_如何学C (
   (user = 'Jim' AND grade = 'B')
   OR (user = 'Tim' AND grade = 'C')
);

But is there a way to do something more like this?

SELECT COUNT(*)
    FROM grades
    WHERE (user, grade) IN (('Jim','B'), ('Tim','C'));

EDIT: As a side note, I'd only tested with:

(user, grade) = ('Tim','C')

And that fails, so I assumed IN would fail as well, but I was wrong (thankfully!).


The query you posted should be valid syntax

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


You could use a subquery to treat a list of tuples like a table:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜