Jump to content

Alternative to using a where clause in a left join nested subquery


idkwhy

Recommended Posts

Hello,
 
I have this SQL here:

SELECT 
      F.`board_id`, F.`board`, F.`desc`, C.`cat`, T.`topic`, T.`aname`, 
      T.`author`, T.`tname`, T.`tdate` 
FROM `f` F 
LEFT JOIN 
     (SELECT 
            `topic`, `tname`, `tboard`, `author`, `tdate`, 
             U.`name` AS `aname` 
      FROM `topics` 
      LEFT JOIN `people` U 
        ON U.`id` = `author` 
      ORDER BY `tupdated` DESC
      LIMIT 1) T 
  ON T.`tboard` = F.`board_id` 
LEFT JOIN `f_cats` C 
  ON C.`cid` = F.`bcat` 
WHERE F.`plevel` <= ? 
AND F.`age` <= ?
ORDER BY C.`cid` ASC

The syntax is fine but the problem is the subquery nested in the left join. It returns only the first row. I realize the LIMIT 1 does that. I tried to make the subquery

     (SELECT 
            `topic`, `tname`, `tboard`, `author`, `tdate`, 
             U.`name` AS `aname` 
      FROM `topics` 
      LEFT JOIN `people` U 
        ON U.`id` = `author` 
      WHERE `tboard` = F.`board`
      ORDER BY `tupdated` DESC
      LIMIT 1) T 

but it says that the F.`board` is an unknown column on the where clause.

 

Not sure what to try next.. is there any alternative I can do? Originally I had the last topic ID saved to the f table as a separate column but the maintainability has been a pain in the butt since I have to update it whenever a topic is created, moved, or bumped..

 

Anyone can help me? It would be much appreciated!

 

Thanks in advance to all replies :D

 

Sorry, I thought it was pretty obvious what I wanted to achieve but in retrospect I realize that it's not as straight forward as I thought

 

I am building my own custom forums and I'm trying to have the last topic posted on the board in the same query that displays the list of all the boards

 

I don't believe that linked solution will help me

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.