【问题标题】:Select attachment_id from one table and user_id in another从一个表中选择 attachment_id 并在另一个表中选择 user_id
【发布时间】:2014-07-12 15:54:24
【问题描述】:

我想从phpbb_attachments 中随机选择 5 个图像,并从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");

【问题讨论】:

  • 你的问题是什么?
  • @GordonLinoff 显示图片附件和作者姓名

标签: mysql sql


【解决方案1】:

你可以试试这个:

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

正如here 所说,最好避免使用ORDER BY RAND()。在那里寻找一些替代品。

另外,我只包含了您显示的字段,您应该对其进行调整以包含文件位置等字段。

最后但同样重要的是,您的示例查询引用了 phpbb3_attachments,并且示例数据使用了不同的名称。你应该相应地调整它。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2020-08-17
    • 2017-11-13
    相关资源
    最近更新 更多