Jump to content

[SOLVED] REALLY bad with JOIN queries...simple help, anyone???


ShootingBlanks

Recommended Posts

Hello, everyone - I'm pretty new to this, so this is probably easy, but I just can't seem to get it right!

 

Here's what I have:

  • "documents" table with columns "doc_id" (primary - INT) and "doc_title" (VARCHAR)
  • "categories" table with columns "cat_id" (primary - INT) and "cat_name" (VARCHAR)
  • "doc_cat_lookup" table with columns "doc_id" (INT) and "cat_id" (INT)

 

Each document may belong to multiple categories.  For example:

  • Documents A, B, & C belong to "category one"
  • Documents D & E belong to "category two"
  • Documents F, G, H, & I belong to "category three"
  • Document J belong to "category four"

 

I need a query so that I select the document names and category names from the "documents" and "categories" table where the doc_ids and cat_ids match up.  So, based on the mock-data that I wrote above, I'd like the results to be:

 

"Category One" - Doc A Title

"Category One" - Doc B Title

"Category One" - Doc C Title

 

"Category Two" - Doc D Title

"Category Two" - Doc E Title

 

"Category Three" - Doc F Title

"Category Three" - Doc G Title

"Category Three" - Doc H Title

"Category Three" - Doc I Title

 

"Category Four" - Doc J Title

 

Thanks!...

 

 

Link to comment
Share on other sites

Okay - I think I have overcomplicated things to the point where I'm getting messed-up again...

 

...here's the query that I have that worked:

SELECT categories.cat_name, doc_cat_lookup.cat_id, doc_cat_lookup.doc_id,
documents.title_full, documents.cat_id, documents.doc_id
FROM documents, categories, doc_cat_lookup
WHERE documents.doc_id = doc_cat_lookup.doc_id
AND categories.cat_id = doc_cat_lookup.cat_id
ORDER BY categories.cat_name, documents.title_full

 

Despite the clutter, that all worked fine.  But now I need to add something else to it...

 

In a similar way that I described how my categories tables would work in my original post, I have now added two USER tables.  They are:

  • "users" - which has columns "user_id" (primary - INT) and "username" (VARCHAR)
  • "doc_user_lookup" - which has columns "doc_id" and "user_id"

 

A document could have any number of users.

 

I need to add to that SELECT query a way to filter it out so that the results are FURTHER limited by the doc_user_lookup.user_id equaling the $_SESSION['user_id'] (which I have stored in the variable $userid).

 

THANKS!!!

 

Link to comment
Share on other sites

You need to add the new tables after the FROM clause and how they should be joined after the WHERE clause. Also, make sure you're properly escaping any variables that are being placed in SQL.

 

I actually just added a parenthetical "AND" statement, and it seemed to work:

AND (doc_user_lookup.user_id = '$userid' AND doc_user_lookup.doc_id = documents.doc_id)

 

So, my finished query is:

SELECT categories.cat_name, doc_cat_lookup.cat_id, doc_cat_lookup.doc_id,
documents.title_full, documents.doc_id, documents.live_doc,
doc_user_lookup.user_id, doc_user_lookup.doc_id
FROM documents, categories, doc_cat_lookup, doc_user_lookup
WHERE documents.doc_id = doc_cat_lookup.doc_id
AND categories.cat_id = doc_cat_lookup.cat_id AND documents.live_doc = 'n' 
AND (doc_user_lookup.user_id = '$userid' AND doc_user_lookup.doc_id = documents.doc_id)
ORDER BY categories.cat_name, documents.title_full

 

Thanks for the help out!!!  ;D

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.