PDA

View Full Version : Check this query please


Logikos
08-17-2004, 05:51 PM
$info = $DB_site->query("
SELECT
thread.threadid as threadid,thread.title as title,thread.forumid as forumid,
thread.postusername as postusername,thread.postuserid as postuserid,thread.dateline as dateline,
thread.views as views,thread.replycount as replycount,forum.title as forumtitle
FROM thread AS thread
WHERE forumid IN($id)
ORDER BY threadid DESC
");


That query is basicly taking alot of info from the table thread. I wanted to grab the forum name from the database. Seeing the thread table don't have the title of forums just ids. I tried getting it from the forum table in the same query. I just added a "forum.title as forumtitle" But i get an sql error

mysql error: Unknown table 'forum' in field list

Any ideas how to grab info from to tables in the same query? Thanks a bunch

Colin F
08-17-2004, 06:27 PM
Try a JOIN:


$info = $DB_site->query("
SELECT
thread.threadid as threadid,thread.title as title,thread.forumid as forumid,
thread.postusername as postusername,thread.postuserid as postuserid,thread.dateline as dateline,
thread.views as views,thread.replycount as replycount,forum.title as forumtitle
FROM thread AS thread
LEFT JOIN " . TABLE_PREFIX . "forum AS forum ON (forum.forumid = thread.forumid)
WHERE forumid IN($id)
ORDER BY threadid DESC
");

Logikos
08-17-2004, 06:45 PM
Ya i tried the LEFT JOIN " . TABLE_PREFIX . "forum AS forum ON (forum.forumid = thread.forumid)

But i kept getting a mysql error: Column: 'forumid' in where clause is ambiguous

Modin
08-17-2004, 10:25 PM
Just need to specify where the forumid field is from. So this will work.

$info = $DB_site->query("
SELECT
thread.threadid as threadid,thread.title as title, thread.forumid as forumid,
thread.postusername as postusername,thread.postuserid as postuserid,thread.dateline as dateline,
thread.views as views,thread.replycount as replycount,forum.title as forumtitle
FROM thread AS thread
LEFT JOIN " . TABLE_PREFIX . "forum AS forum ON (forum.forumid = thread.forumid)
WHERE forum.forumid IN($id)
ORDER BY threadid DESC


However, if this script has access to $forumcache you don't need to select the forum title from the table, and therefore no need for a join.

such as...

$info = $DB_site->query("
SELECT
thread.threadid as threadid,thread.title as title, thread.forumid as forumid,
thread.postusername as postusername,thread.postuserid as postuserid,thread.dateline as dateline,
thread.views as views,thread.replycount as replycount,
FROM thread AS thread
WHERE thread.forumid IN($id)
ORDER BY threadid DESC");

while($item = $DB_site->fetch_array($info))
{
$item['forumtitle'] = $forumcache["$item[forumid]"]['title'];
//rest of your code
}


Both will have the same results, but the second would be quicker because there's no join. (this is of course if the script has access to $forumcache)

Logikos
08-18-2004, 03:44 AM
Ok this is what i placed


$info = $DB_site->query("
SELECT
thread.threadid,thread.title,thread.forumid,thread .postusername,thread.postuserid,
thread.dateline,thread.views,thread.replycount,for um.title as forumtitle
FROM thread AS thread
LEFT JOIN " . TABLE_PREFIX . "forum AS forum ON (forum.forumid = thread.forumid)
WHERE forumid IN($id)
ORDER BY threadid DESC
");



And i get this

mysql error: Column: 'forumid' in where clause is ambiguous

mysql error number: 1052

Fixed it. Had to chage the WHERE statement to

WHERE thread.forumid IN($id)