0

I want to select 5 random image from phpbb_attachments and show author username name from phpbb_users.

phpbb_attachments:        phpbb_topics_posted:        phpbb_users:

attach_id  topic_id       user_id  topic_id           user_id  username
1          10             21       10                 21       Tom
2          5              53       5                  53       Maria
3          15             11       15                 11       John

$result = mysqli_query($con,"SELECT * FROM phpbb3_attachments WHERE mimetype = 'image/jpeg' ORDER BY RAND() LIMIT 5");

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131

1 Answers1

0

You can try this:

SELECT att.attach_id,
       u.username
FROM phpbb_attachments att
JOIN phpbb_topics_posted t ON (t.topic_id=att.topic_id)
JOIN phpbb_users u ON (u.user_id=t.user_id)
WHERE att.mimetype='image/jpeg'
ORDER BY rand() LIMIT 5

As it says here, it is best to avoid ORDER BY RAND(). Look there for some alternatives.

Also, I only included the fields you showed, you should tweak it to include fields such as file location,etc.

Last, but not least, your example query references phpbb3_attachments, and the sample data uses a different name. You should adjust that accordingly.

Hope this helps.

Community
  • 1
  • 1
Mariano D'Ascanio
  • 1,202
  • 2
  • 16
  • 17