Solved

Help with SQL query

Posted on 2012-03-25
2
245 Views
Last Modified: 2012-03-25
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
Comment
Question by:SheppardDigital
2 Comments
 
LVL 8

Accepted Solution

by:
fundacionrts earned 500 total points
ID: 37762990
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
 

Author Closing Comment

by:SheppardDigital
ID: 37763107
Perfect, thank you
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question