insert into #variables.tableprefix#conferences(id,name,description,active) values(, , , ) delete from #variables.tableprefix#conferences where id = delete from #variables.tableprefix#subscriptions where conferenceidfk = select id, name, description, active from #variables.tableprefix#conferences where id = select #variables.tableprefix#conferences.id,#variables.tableprefix#conferences.name, #variables.tableprefix#conferences.description, #variables.tableprefix#conferences.active, Count(#variables.tableprefix#messages.id) AS messagecount, max(#variables.tableprefix#messages.posted) as lastpost, ( select threadidfk from #variables.tableprefix#messages m where m.posted = (SELECT max(mm.posted) as lastpost from ((#variables.tableprefix#conferences c left JOIN #variables.tableprefix#forums f ON c.id = f.conferenceidfk) left join #variables.tableprefix#threads t ON f.id = t.forumidfk) left JOIN #variables.tableprefix#messages mm ON t.id = mm.threadidfk where c.id = #variables.tableprefix#conferences.id ) ) as threadidfk, ( select useridfk from #variables.tableprefix#messages m where m.posted = (SELECT max(mm.posted) as lastpost from ((#variables.tableprefix#conferences c left JOIN #variables.tableprefix#forums f ON c.id = f.conferenceidfk) left join #variables.tableprefix#threads t ON f.id = t.forumidfk) left JOIN #variables.tableprefix#messages mm ON t.id = mm.threadidfk where c.id = #variables.tableprefix#conferences.id ) ) as useridfk from ((#variables.tableprefix#conferences left JOIN #variables.tableprefix#forums ON #variables.tableprefix#conferences.id = #variables.tableprefix#forums.conferenceidfk) left JOIN #variables.tableprefix#threads ON #variables.tableprefix#forums.id = #variables.tableprefix#threads.forumidfk) left JOIN #variables.tableprefix#messages ON #variables.tableprefix#threads.id = #variables.tableprefix#messages.threadidfk where #variables.tableprefix#conferences.active = 1 GROUP BY #variables.tableprefix#conferences.id,#variables.tableprefix#conferences.name, #variables.tableprefix#conferences.description, #variables.tableprefix#conferences.active order by #variables.tableprefix#conferences.name select top 20 #variables.tableprefix#messages.title, #variables.tableprefix#threads.name as thread, #variables.tableprefix#messages.posted, #variables.tableprefix#users.username, #variables.tableprefix#messages.threadidfk as threadid, #variables.tableprefix#messages.body from #variables.tableprefix#messages, #variables.tableprefix#threads, #variables.tableprefix#users, #variables.tableprefix#forums where #variables.tableprefix#messages.threadidfk = #variables.tableprefix#threads.id and #variables.tableprefix#messages.useridfk = #variables.tableprefix#users.id and #variables.tableprefix#threads.forumidfk = #variables.tableprefix#forums.id and #variables.tableprefix#forums.conferenceidfk = order by #variables.tableprefix#messages.posted desc limit 20 update #variables.tableprefix#conferences set name = , description = , active = where id = select id, name, description from #variables.tableprefix#conferences where active = 1 and ( (name like or description like '%#aTerms[x]#%') #joiner# name like or description like '%#arguments.searchTerms#%' )