doulin8374 2016-06-19 19:29
浏览 97
已采纳

如何在php中连接两个表并返回结果

I have two tables namely: users and messages

I want to JOIN messages table with users table and select users information from users table where the id in users table has NO row in messages table checking with the hash column.

Please Note: user_two column in messages table is the ids from users table

I tried but it return no result. Please help:

<?php 

        //Get the friend a user wants to send message
    if(isset($_POST['recipname']) && !empty($_POST['recipname'])){
        $recipname  =   mysqli_real_escape_string($dbc_conn,htmlentities(trim($_POST['recipname'])));

        $message_group_tatable = "messages";

        $sql    =   "

        SELECT      users.id, users.username,users.FirstName ,
                    users.LastName , users.avatar ,
                    users.cell_group

        FROM        users 
        INNER JOIN  $message_group_tatable 
        ON          $table_name.id=$message_group_tatable.user_two

        WHERE       $message_group_tatable.hash = NULL
        AND         users.id    != $message_group_tatable.user_two
        AND         users.username 
        LIKE        '%$recipname%'   
        LIMIT       6


        ";

        $query  =   mysqli_query($dbc_conn,$sql);
        //die(mysqli_error($dbc_conn));
        if(mysqli_num_rows($query) > 0){
        while($row  =   mysqli_fetch_array($query)){
        $name   =   ucfirst($row['FirstName'])." ".ucfirst($row['LastName']);
        $user_id    =   $row['id'];
        $user_name  =   $row['username'];
        $school     =   $row['cell_group'];
        $avatar     =   $row['avatar'];


        ?>
        <div class="selectmeWrapper this">
        <table class="selectme">
          <tr>
            <td><span class="selectmeavtspan"><img class="selectmeavatar" src="uploaded/<?php echo $avatar; ?>" /></span></td>
            <td><span class="univ"><?php echo $name; ?></span></td>
          </tr>
        </table>

    <span class="uiremovable selected" title="pro/<?php echo $user_name;?>">
        <span> <img class="recipavt" src="uploaded/<?php echo $avatar; ?>" /></span>
        <span class="selectedName">
            <?php echo $name; ?>
        <input type="hidden" autocomplete="off" 
            value="<?php echo $user_name ?>" />

    </span>
    <a href="#" id="<?php echo $user_name?>" class="ulCloseSmall <?php echo "Remove ".$name; ?>"><i class="fa fa-times"></i></a>
    </span>
        </div>
        <?php


        }

        }else{
        echo "<p class='noresult'>No Result Found.</p>";    

        }


        }


?>

Table structure for table messages

        CREATE TABLE IF NOT EXISTS `messages` (
      `user_one` int(11) NOT NULL,
      `user_two` int(11) NOT NULL,
      `hash` int(11) DEFAULT NULL,
      `id` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;

and Table structure for table users

    CREATE TABLE IF NOT EXISTS `users` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(64) DEFAULT NULL,
      `FirstName` varchar(32) DEFAULT NULL,
      `LastName` varchar(32) DEFAULT NULL,
      `Email` varchar(64) DEFAULT NULL,
      `Password` varchar(32) DEFAULT NULL,
      `Month` varchar(6) DEFAULT NULL,
      `Day` varchar(6) DEFAULT NULL,
      `Year` varchar(11) DEFAULT NULL,
      `Gender` varchar(6) DEFAULT NULL,
      `cell_group` varchar(100) DEFAULT NULL,
      `active` varchar(11) DEFAULT NULL,
      `avatar` text,

      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
  • 写回答

1条回答 默认 最新

  • dongyinzheng6572 2016-06-19 19:35
    关注

    You could use a not in clause

       $sql    =   "
        SELECT      users.id, users.username,users.FirstName ,
                    users.LastName , users.avatar ,
                    users.cell_group
    
        FROM        users 
        WHERE       users.id  not in (select distinct user_two from " . $message_group_tatable . " )
        AND         users.username 
        LIKE        concat('%', ". $recipname .", '%')   
        LIMIT       6 
      ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。