idkwhy Posted March 17, 2015 Share Posted March 17, 2015 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 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted March 17, 2015 Solution Share Posted March 17, 2015 You don't say what you want to achieve but I suspect your problem may be similar to this one http://forums.phpfreaks.com/topic/295214-joining-two-tables-and-returning-fields-from-maxid-row/?do=findComment&comment=1508035 1 Quote Link to comment Share on other sites More sharing options...
idkwhy Posted March 17, 2015 Author Share Posted March 17, 2015 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 Quote Link to comment Share on other sites More sharing options...
idkwhy Posted March 17, 2015 Author Share Posted March 17, 2015 Actually my apologies, that did work! Thank you so much for the help!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.