Solved

Help with SQL query

Posted on 2012-03-25
2
242 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now