dtk31564 2015-08-10 14:47
浏览 34
已采纳

在两个表之间使用id来输出所需数据

I have two database tables I am trying to have work together. They are called users and forun_topics

In forum_topics I have a column called topic_creator. This will always be an integer.

In users I have a column called id.

The id from the users table is what the topic_creator integer is.

So What I am trying to accomplish is to associate the topic_creator with the id in the users table and then with that find another column I have in my users table called username.

So to make this very simple I am trying to output the user name from the person who made a post.

As of now this is my query I have that displays the topic_creator, which I can figure out how to output everything once the SQL is working, but I can't figure out how to add another database table to this query and then find the username field. The largest part I am in question is how to get the username field after.

$query2 = mysqli_query($con,"SELECT t.*, c.id AS cid FROM forum_topics AS t, forum_categories AS c ORDER BY topic_reply_date DESC LIMIT 3")

How can I do this?

UPDATE to show more code

$query2 = mysqli_query($con,"SELECT t.*, c.id AS cid 
FROM forum_topics AS t
INNER JOIN forum_categories AS c 
 on t.categories.ID = c.ID
INNER JOIN users u 
 on u.id = t.topic_creator
ORDER BY topic_reply_date DESC LIMIT 3")
    or die ("Query2 failed: %s
".($query2->error));
    $numrows2 = mysqli_num_rows($query2);
    if($numrows2 > 0){

    $topics .= "<table class='top_posts_table'>";
    //Change link once discussion page is made
    $topics .= "<tr><th class='top_posts_th'>Topic Title</th><th class='top_posts_th'>Replies</th><th class='top_posts_th'>Views</th></tr>";
    $topics .= "<tr><td colspan='3'><hr /></td></tr>";
    while($row2 = mysqli_fetch_assoc($query2)){
        $cid = $row2['cid'];
        $tid = $row2['id'];
        $title = $row2['topic_title'];
        $views = $row2['topic_views'];
        $date = $row2['topic_date'];
        $date = fixDate($date);
        $creator = $row2['username'];
        $topics .= "<tr><td class='top_posts_td'><a href='forum_view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted 
        by: ".$creator."<br> on ".$date."</span></td><td class='top_posts_td'>0</td><td align='center'>".$views."</td></tr>";
        $topics .= "<tr><td colspan='3'><hr /></td></tr>";
    }
  • 写回答

2条回答 默认 最新

  • dtsi9484 2015-08-10 15:06
    关注

    I'm not sure what your SQL level is, but this is something usually of basic level. I suggest before continuing with your project, stop for a second and go read something about it, at least to have a basic understanding of the topic. It's one of the most common operations, and having a grasp of how this works will always be better than some anwser on StackOverflow, especially because it's something you'll likely encounter a lot.

    What you're looking for is something like this:

    SELECT * FROM forum_topics
    INNER JOIN users
    ON forum_topics.topic_creator = users.id
    ORDER BY forum_topics.topic_reply_date DESC
    LIMIT 3
    

    This can be improved in a number of ways, for instance giving aliases to table names and specifying the actual columns you want (where I left a *).

    If I may, try to be consistent with naming. In your example, "topic_creator" could become "user_id", which in these situations is the norm. In general, where you have a 1:N relationship between two tables, try to name the foreign key something like "user_id" (associated to users.id), "topic_id" (topics.id) etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭