Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Help with SQL query

I'm having some problems with a query I'm trying to put together.

I have the following tables.

FOLDERS
id
name
trash
parent_id
create_user_id

PERMISSION
id
user_id
folder_id

What I want to do is list all folders where the user has an entry in the PERMISSIONS table for.

So there are two conditions

1) List folders where the parent_id = a supplied ID, and where the user has an entry in the PERMISSIONS table
2) List folders where the user isn't the creator of the folder, but is in the PERMISSIONS folder

This is what I've got so far...
SELECT f.id,f.name,f.trash,f.parent_id,f.create_user_id,f.create_date FROM folders AS f, folder_permissions AS p WHERE f.trash = 0 AND p.user_id = '".$user_id."' AND p.folder_id = f.id  AND (f.parent_id = '".$parent_id."' OR p.user_id != f.create_user_id) GROUP BY f.id ORDER BY f.name ASC

Can anyone point out where I might be going wrong?
0
SheppardDigital
Asked:
SheppardDigital
1 Solution
 
fundacionrtsAdministrador de SistemasCommented:
SELECT DISTINCT
      f.id,
      f.name,
      f.trash,
      f.parent_id,
      f.create_user_id,
      f.create_date
FROM
      folders AS f
            INNER JOIN folder_permissions AS p ON p.folder_id = f.id
WHERE
      f.trash = 0 AND
      p.user_id = '".$user_id."' AND
      (f.parent_id = '".$parent_id."' OR p.user_id != f.create_user_id)
ORDER BY
      f.name ASC
0
 
SheppardDigitalAuthor Commented:
Perfect, thank you
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now