0

I have 2 tables :

Table reports

id   |  name
-------------
1    |  test 1
2    |  test 2
3    |  test 3

Table reports_access

id_table  |   group_id
-----------------------
1         |   1
1         |   2
1         |   3
1         |   4
2         |   1
2         |   2

I need to access reports depending on group_id of the logged user, a user belongs to multiple groups.

I tried :

SELECT reports.*
    FROM reports
WHERE (
    SELECT group_id 
        FROM reports_access AS repacc 
        WHERE repacc.id_table = reports.id
    ) IN (1, 3) 

I got this error :

Subquery returns more than 1 row

I can't understand if I can do what I want using one request, because I need to test if an array belongs to other array.

Did I miss something ?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • Does `(1, 3)` in the subquery represent a user id or a group_id? Either way I think you want a `join` + a `where` not a subquery. – Hunter McMillen Mar 12 '18 at 20:22

2 Answers2

2

I think you are looking for this:

 SELECT reports.*
        FROM reports
    WHERE id in (
        SELECT repacc.id_table 
            FROM reports_access   
           where group_id
        IN (1, 3) )
Ole EH Dufour
  • 2,968
  • 4
  • 23
  • 48
0

How about using JOIN ?

SELECT DISTINCT r.*
FROM reports r 
INNER JOIN reports_access ra 
on ra.id_table=r.id 
where ra.group_id in (1,3)
stackFan
  • 1,528
  • 15
  • 22