Jump to content

[SOLVED] JOINing dilemma -- pls assist where possible.


Recommended Posts

SELECT q.quote, q.qid, qp.paraphrase, s.*, u.username
FROM quotes q
LEFT JOIN quotes_paraphrases qp ON ( q.qid=qp.quote_id )
LEFT JOIN sources s ON ( q.source_id=s.sid )
LEFT JOIN users u ON ( q.user_id=u.uid )
WHERE q.enabled=1
AND q.qid=$rand

 

my problem is that i have a numeric field in both the quotes table and the quotes_paraphrases table that are called user_id.  The problem is that the person who submitted the quote and the person who submitted the paraphrase of that quote are not always the same.  Is there a way to modify this SQL statement so that I can pull both the quotes.user_id=users.uid AND quotes_paraphrases.user_id=users.uid?

 

I've tried inter-mixing an INNER join in the statement above but that gave me syntax errors (had to try), I've tried just amending the last LEFT JOIN to look like: (LEFT JOIN users u ON ( q.user_id=u.uid AND qp.user_id=u.uid) but that just adds a bunch of NULL values to the column of the result set that used to hold the user who submitted the quote where there wasn't an associated paraphrase to that particular quote. 

 

All help is greatly appreciated.

SELECT q.quote, q.qid, qp.paraphrase, s.*, qu.username AS quote_username, qpu.username AS para_username

FROM quotes q

LEFT JOIN quotes_paraphrases qp ON ( q.qid=qp.quote_id )

LEFT JOIN sources s ON ( q.source_id=s.sid )

LEFT JOIN users qu ON ( q.user_id=qu.uid )

LEFT JOIN users qpu ON ( q.user_id=qpu.uid )

WHERE q.enabled=1

AND q.qid=$rand

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.