【问题标题】:Joining two tables to match integers to get a username连接两个表以匹配整数以获取用户名
【发布时间】:2015-07-24 17:55:10
【问题描述】:

我无法弄清楚如何连接两个表和 SELECT 来自不同数据库表的不同行。

我想添加我的users 表以匹配id 和post_creator,然后从users 表中获取username,只要id 和post_creator 号码匹配。

因此,如果我的 users 表的 id 为 10,那就是 Jim 的用户名。如果 post_creator 值为 10,我希望 Jim 被找到并回显。

现在这正在杀死代码,我不知道我做错了什么。

while ($row = $stmt->fetch()) {
        //added in topic title variable
        $topic_title;
//  foreach($stmt as $row) {
        //Prepared SELECT stmt to get forum posts
        $stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
        FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
        if($stmt2===false) {
            die();
        } else {
        //var_dump($stmt2);

            $stmt2->bind_param("ii", $cid, $tid);
            $stmt2->execute();
            $stmt2->store_result();
            $stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
            if (!$stmt2) {
            throw new Exception($con->error);
            }
        }
    $num_rows2 = $stmt2->num_rows;
    if($num_rows2) {
        $count2=0;
        while($stmt2->fetch()) {
        $count2++;
        $post_id;
        $post_category_id;
        $post_topic_id;
        $post_creator;
        $post_content;
         $post_date = fixDate($post_date);
        //$post_date;
            echo "<tr><td valign='top' style='border: 1px solid #000000;'>
            <div style='min-height: 125px;'>".$topic_title. "Post ".$count2."<br />
            by ".$post_creator." - " .$post_date. "<hr />" . $post_content ."</div></td>
            <td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
            <tr><td colspan='2'><hr /></td></tr>";
        }

【问题讨论】:

    标签: php mysql sql join


    【解决方案1】:

    你可以加入表格...比如这个(加入用户、类别和主题)

    SELECT forum_posts.id post_id, forum_posts.category_id post_category_id, forum_posts.topic_id post_topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date, forum_post_categories.category_name, forum_post_topics.topic_name, users.id user_id, users.username user_username FROM forum_posts LEFT JOIN users ON users.id = forum_posts.post_creator RIGHT JOIN forum_post_categories ON forum_post_categories.id = forum_posts.category_id RIGHT JOIN forum_post_topics ON forum_post_topics.id = forum_posts.topic_id WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?

    注意:这是未经测试的。

    【讨论】:

    • 我怎样才能让它看看post-creator = users.id?我在这里发布的查询最初只是为了从 forum_posts 数据库中获取信息,所以最好进行一个单独的查询并查找我需要的信息,例如 forum_posts.post_creator 和 users.id?
    • 我不明白你的意思。这是将用户名加入结果集——它们总是匹配的。您确实有 users.id 的 where 子句...也许您可以删除它并看看我在说什么。你会得到类似的东西:13 | john_doe | 123 | 12 | 45 | 13 | some contents | 2015-07-24 | category name | topic name
    【解决方案2】:
    select u.*,fp.*  from users u inner join forum_posts fp u.id=fp.post_creator
    where  fp.category_id=? AND fp.topic_id=? AND fp.post_creator=? AND  u.id=?
    

    更新查询:

    select u.user_name,fp.post_creator from 
    users u inner join forum_posts fp u.id=fp.post_creator
    

    检查这个更新的,它只会给你 user_name 和 post_creator

    【讨论】:

    • 我怎样才能让它看看post-creator = users.id?我在这里发布的查询最初只是为了从 forum_posts 数据库中获取信息,所以最好做一个单独的查询并查找我需要的信息,例如 forum_posts.post_creator 和 users.id
    • 您是否只想匹配没有任何 where 条件的行?
    • 可能是的......但我仍然需要 cid 和 tid 的 where 语句,因为我将其用于文件中的其他内容。所以这就是为什么我在想我是否应该做一个单独的查询?我只想找到 post_creator 与 users.id 匹配的位置,然后从中删除用户名并以与我现在的代码类似的方式发布。 (while循环)
    • 我这样做是为了看看它是否可以工作...$test = "SELECT u.user_name,fp.post_creator from users u inner join forum_posts fp u.id=fp.post_creator"; echo $test; 并得到回应...SELECT u.user_name,fp.post_creator from users u inner join forum_posts fp u.id=fp.post_creator
    【解决方案3】:

    尝试删除此代码部分

            if($stmt2===false) {
                die();
            } else {  }
    

    简单地做:

     $stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
            FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
    $stmt2->bind_param("ii", $cid, $tid);
                $stmt2->execute();
                $stmt2->store_result();
                $stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
                if (!$stmt2) {
                throw new Exception($con->error);
                }
    

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 2013-11-11
      • 2015-03-16
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      相关资源
      最近更新 更多