【问题标题】:How to join two tables and return result in php如何连接两个表并在php中返回结果
【发布时间】:2016-10-21 00:44:41
【问题描述】:

我有两个表,即:usersmessages

我想将messages 表与users 表连接起来,并从users 表中选择用户信息,其中users 表中的ID 在messages 表中没有行,并使用哈希列进行检查。

请注意:messages 表中的user_two 列是来自users 表的ID

我试过了,但没有返回任何结果。 请帮忙:

<?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>";    

        }


        }


?>

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 ;

和表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 ;

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    你可以使用 not in 子句

       $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 
      ";
    

    【讨论】:

    • 绑定您的代码时出错,我使用 mysqli_error() 函数检查出了什么问题。当我提交它说的表格时:'where子句'中的未知列'jaa'
    • 我已经使用字符串 concat 更新了 asnwer .. 所以错误可能与 $message_group_tatable vars 有关,最终尝试 var:dump($message_group_tatable) 获取真实内容
    • 感谢我尝试编辑您的代码,它按我的意愿工作。不知道子查询可以解决这个问题。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多