多值MySQL子查询
我有两张桌子: 表格报告多值MySQL子查询,mysql,semi-join,Mysql,Semi Join,我有两张桌子: 表格报告 id | name ------------- 1 | test 1 2 | test 2 3 | test 3 id_table | group_id ----------------------- 1 | 1 1 | 2 1 | 3 1 | 4 2 | 1 2 | 2 表格报告\u访问 id | nam
id | name
-------------
1 | test 1
2 | test 2
3 | test 3
id_table | group_id
-----------------------
1 | 1
1 | 2
1 | 3
1 | 4
2 | 1
2 | 2
表格报告\u访问
id | name
-------------
1 | test 1
2 | test 2
3 | test 3
id_table | group_id
-----------------------
1 | 1
1 | 2
1 | 3
1 | 4
2 | 1
2 | 2
我需要根据登录用户的组id访问报告,一个用户属于多个组
我试过:
SELECT reports.*
FROM reports
WHERE (
SELECT group_id
FROM reports_access AS repacc
WHERE repacc.id_table = reports.id
) IN (1, 3)
我得到了这个错误:
子查询返回超过1行
我无法理解使用一个请求是否可以实现我想要的,因为我需要测试一个数组是否属于另一个数组
我错过什么了吗?我想你在找这个:
SELECT reports.*
FROM reports
WHERE id in (
SELECT repacc.id_table
FROM reports_access
where group_id
IN (1, 3) )
我想你正在寻找这个:
SELECT reports.*
FROM reports
WHERE id in (
SELECT repacc.id_table
FROM reports_access
where group_id
IN (1, 3) )
使用
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)
使用
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)
子查询中的
(1,3)
表示用户id还是组id?无论是哪种方式,我都认为您需要一个连接
+a其中
不是子查询。子查询中的(1,3)
表示用户id还是组id?无论哪种方式,我都认为您需要一个连接
+awhere
而不是子查询。这正确地实现了半连接。其他答案(到目前为止)导致重复行。这正确地实现了半连接。其他答案(到目前为止)会产生重复的行。