Solved

Help with SQL query

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 72
selecting date modified field from a table 2 44
Create a Select Query and Populate a Table 3 48
insert row field data graphically 4 23
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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