Jump to content

[SOLVED] Complex SELECT (for me, at least)


Luth

Recommended Posts

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.  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.