MyBB Community Forums

Full Version: Trying to get a complex query right.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is what I am trying to do.

I have 3 tables...I need to join them

First table = vb_itrader

Second table = vb_user

Thrid table = mybb_users

Now here is the hard part. I need to correlate the vb_itrader with the vb_user.username column and THEN I need to match that result and join the third column based on the vb_user.username ON mybb_users.username and fetch the mybb_users.uid.

It's a doozy...I am attempting to recover the itrader rating from my VB and place it into the reputation system for mybb. It's not fun at all. However I assume this needs to be done eventually to pull the reputations and it will be similar to this. I could do reputations but I prefer to pull the itrader for now. Neither one has the username in the table. VB has only the userid.

Anyone got a clue on this?

This is what I have so far...but it's 2 seperate queries and I can't get them intermixed and then pull from the mybb_users table the username column. It's a cluster f***.

//taker
SELECT vb_user.username AS usertake,vb_itrader.rating,vb_itrader.subject,vb_itrader.dateline FROM vb_user
  LEFT JOIN vb_itrader ON vb_user.userid=vb_itrader.rateduserid
  LEFT JOIN mybb_user ON vb_user.username=mybb_user.username;
  WHERE vb_itrader.userid IS NOT NULL

//giver
SELECT vb_user.username AS usergiver,vb_itrader.rating,vb_itrader.subject,vb_itrader.dateline FROM vb_user
  LEFT JOIN vb_itrader ON vb_user.userid=vb_itrader.userid
  WHERE vb_itrader.userid IS NOT NULL
Untested, and not really checked, but I guess the idea would be something like:
SELECT vb_user.username AS usergiver,vb_itrader.rating,vb_itrader.subject,vb_itrader.dateline FROM vb_user, vb_itrader
WHERE vb_itrader.userid IS NOT NULL
AND
(
 (vb_user.userid=vb_itrader.rateduserid AND vb_user.username IN 
  (SELECT mybb_user.username FROM mybb_user)
 )
 OR
 (vb_user.userid=vb_itrader.userid)
)