PDA

View Full Version : How can I combine 2 query's?


Sarcoth
03-23-2007, 07:38 PM
Here is the main query I am using.

$users = $db->query_read_slave("
SELECT user.*, usergroup.usergroupid, usergroup.title, user.options, usertextfield.*, userfield.*,
IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid, infractiongroupid
FROM " . TABLE_PREFIX . "usergroup AS usergroup
LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.usergroupid = usergroup.usergroupid OR FIND_IN_SET(usergroup.usergroupid, user.membergroupids))
LEFT JOIN " . TABLE_PREFIX . "userfield AS userfield ON(userfield.userid = user.userid)
LEFT JOIN " . TABLE_PREFIX . "usertextfield AS usertextfield ON(usertextfield.userid=user.userid)
");

I have a new table called roster in my database. It has a field in common with userfield table from above. 'userfield.field7' is equal to 'roster.CharID'. The following is the query I'm using for the new table.

$query = $vbulletin->db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "roster
");

I've tried a bunch of things and for some reason, I just can't get these two combined into one. Does anyone know what I should do to get them combined? I just want to have one query so I don't have to nest the 2nd query somewhere within the first array.

Any help would be appreciated.

Thanks.

MarkPW
03-23-2007, 07:57 PM
Is this what you're after?


$users = $db->query_read_slave("
SELECT user.*, roster.*, usergroup.usergroupid, usergroup.title, user.options, usertextfield.*, userfield.*,
IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid, infractiongroupid
FROM " . TABLE_PREFIX . "usergroup AS usergroup
LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.usergroupid = usergroup.usergroupid OR FIND_IN_SET(usergroup.usergroupid, user.membergroupids))
LEFT JOIN " . TABLE_PREFIX . "userfield AS userfield ON(userfield.userid = user.userid)
LEFT JOIN " . TABLE_PREFIX . "usertextfield AS usertextfield ON(usertextfield.userid=user.userid)
LEFT JOIN " . TABLE_PREFIX . "roster AS roster ON(roster.CharID=userfield.field7)
");

Sarcoth
03-23-2007, 08:50 PM
Thanks Mark. I don't know what I was doing wrong on mine, but I could never get it to work. So far, it at least isn't giving the usual database error I was getting on all my tests. Now I'll try to test it out with an array.

Thanks again.

So far so good. I performed a simple array and it's working. I'll do further tests this weekend.