I have two MySql Tables as follows:
resource
-----------------------------------------------
id name group owner_id
-----------------------------------------------
1 MyResource1 hs 11
2 MyResource2 ms 24
3 MyResource3 ps 11
...
resource_access
-----------------------------------------------
id resource_id user_id
-----------------------------------------------
1 1 12
2 2 24
3 2 11
4 3 15
...
Now, the first table is a list of resources, of course, and their respective owners in the owner_id
column. The second table is the result of "sharing" this resource with another user. The table resource_access
may contain records with a user_id
that is equivalent to the owner_id
in a row of the resource_access
as a result of messy cleanup from an owner exchange.
I simply want to get the id, name, and group of any resource that a user has access to, whether they are the owner or it has been shared with them. Here is my MySQL query for an example user (24):
SELECT resource.id, resource.name, resource.group
FROM `resource`
INNER JOIN resource_access ON (
resource.owner_id='24'
OR (
resource_access.user_id='24' AND
resource_access.resource_id=resource.id
)
)
Right now, it returns the id
, name
, and group
for resource number 2 multiple times (like twelve). Is there a possible cause for this? I have tried LEFT
and RIGHT
joins and am getting the same result. There are many records in the resource
table, but none with the id
of 2. There are no duplicate rows in resource_access
sharing the resource with the same user twice.
Thanks in advance.