Luth Posted June 12, 2009 Share Posted June 12, 2009 I'm making a heavy duty mod to PHPBB, and it's pushing my limited knowledge of MySQL (5.0). What I want is to select: 1) everything from phpbb_topics [phpbb_topics.*], 2) the user email associated with the first poster [phpbb_users.user_email as first_poster_email], 3) the user email associated with the last poster [phpbb_users.user_email as last_poster_email]. The closest thing I've got is: SELECT t.*, phpbb_users.user_email as first_poster_email FROM ( phpbb_topics t ) LEFT JOIN phpbb_users ON t.topic_poster=phpbb_users.user_id Obviously it's lacking, as there's not even a bit for selecting the last poster email. The user id's are stored in phpbb_topics as 'topic_poster' and 'topic_last_poster_id'. I'm not sure how to proceed with this. Any help would be nice, or even just pointers in the right direction. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/ Share on other sites More sharing options...
Luth Posted June 14, 2009 Author Share Posted June 14, 2009 Is there any more information that I could provided that would assist you in assisting me with this hang-up? I'm afraid I haven't yet found a solution elsewhere, either. Kind regards. Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-855269 Share on other sites More sharing options...
fenway Posted June 15, 2009 Share Posted June 15, 2009 Try doing (2) and (3) independently. Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-856466 Share on other sites More sharing options...
Luth Posted June 16, 2009 Author Share Posted June 16, 2009 That is what I've done, and done an array merge in PHP to combine the results. If there is no way to do them in one operation, then I guess that's as good as it gets, hm? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-856780 Share on other sites More sharing options...
roopurt18 Posted June 16, 2009 Share Posted June 16, 2009 select a.*, (select user_email from phpbb_users b where a.topic_poster=b.user_id) as first_poster_email, (select user_email from phpbb_users c where a.topic_last_poster_id=c.user_id) as last_poster_email from phpbb_topics a Or maybe: select a.*, b.user_email as first_poster_email, ifnull( c.user_email, b.user_email ) as last_poster_email from phpbb_topics a inner join phpbb_users b on a.topic_poster=b.user_id left join phpbb_users c on a.last_poster_id=c.user_id Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-856826 Share on other sites More sharing options...
Luth Posted June 16, 2009 Author Share Posted June 16, 2009 Wow. Okay, I could follow the first one, but that second one throws me for a loop. :-) At any rate, I'll try them and get back to you. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-856861 Share on other sites More sharing options...
kickstart Posted June 16, 2009 Share Posted June 16, 2009 Hi Not sure which version of phpBB you are using, but assuming 2 (which I am still using) then the topics table contains the post_id of the first and last posts, so you can JOIN to the users table via the post table. Something like this:- SELECT a.*, c.user_email as first_poster_email, e.user_email as last_poster_email FROM phpbb_topics a JOIN phpbb_posts b ON a.topic_first_post_id = b.post_id JOIN phpbb_users c ON b.poster_id = c.user_id JOIN phpbb_posts d ON a.topic_last_post_id = d.post_id JOIN phpbb_users e ON d.poster_id = e.user_id All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-856999 Share on other sites More sharing options...
Luth Posted June 17, 2009 Author Share Posted June 17, 2009 Both Kickstart and Roopurt's first select statements worked peachy. Thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/161990-solved-complex-select-for-me-at-least/#findComment-858518 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.