Jump to content

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


idkwhy
Go to solution Solved by Barand,

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.