Pass multiple sets or arrays of values to a function

后端 未结 2 1755
悲&欢浪女
悲&欢浪女 2020-12-22 00:53

I\'m writing a PL/pgSQL function in PostgreSQL 9.3.10 to return who has attended certain classes/sessions from the following table:

Attendance
+-------+-----         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 01:24

    If you pass an array of records to the function it is simple:

    with attendance (class, section, name) as(values
        (1, 1, 'Amy'),
        (1, 1, 'Bill'),
        (1, 2, 'Charlie'),
        (1, 2, 'Dan'),
        (2, 1, 'Emily'),
        (2, 1, 'Fred'),
        (2, 2, 'George')
    )
    select *
    from attendance
    where (class, section) = any(array[(1,1),(2,2)])
    ;
     class | section |  name  
    -------+---------+--------
         1 |       1 | Amy
         1 |       1 | Bill
         2 |       2 | George
    

提交回复
热议问题