MyBB Community Forums

Full Version: Selecting more than 1 table in the same query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
$query = $db->query("
SELECT * FROM ".TABLE_PREFIX."myawards_users ORDER BY awutime DESC LIMIT 25
");

So here I'm selecting the myawards_users table but I need to call the myawards_users table and myawards table.

How can I do this?
Google "MYSQL Left Join"
(2013-06-07, 09:30 PM)Jambuster Wrote: [ -> ]Google "MYSQL Left Join"

I don't really understand that. :s
^ suggestion is to find MySQL codes related to LEFT JOIN for joining the tables and getting the query results
Example? here:

$query = $db->query("
	   SELECT t.tid, t.fid, t.subject, t.lastpost, 
	   t.lastposter, t.lastposteruid, f.name,
	   u.usergroup, u.displaygroup
	   FROM ".TABLE_PREFIX."threads AS t
       INNER JOIN ".TABLE_PREFIX."forums as f
	   ON (f.fid = t.fid)
	   LEFT JOIN " . TABLE_PREFIX . "users AS u 
	   ON (t.lastposteruid = u.uid)
	   AND t.visible = '1'
           GROUP BY t.tid");
Still not a scoobie what to do. :s
This will select every myawards_users row, then select every matching row from myawards with the same 'awutime' value. I don't know if this column is used in both, so you'd better go with using something like award uid or whatever (view in database which columns are same in both tables).

$query = $db->write_query("
SELECT * FROM ".TABLE_PREFIX."myawards_users AS ma_u 
LEFT JOIN ".TABLE_PREFIX."myawards AS ma ON (ma_u.awutime = ma.awutime)
ORDER BY awutime DESC 
LIMIT 25
");