我如何从2个数据库表中选择并限制40个内容?

时间:2016-05-29 05:31:54

标签: php mysql

目前我有3个表我想选择2个表ID并限制显示内容

表名为聊天

id | user_id | chat | date
 1     1         a     2016-05-29 12:02:58

表名为chatroom_chat

chatroom_id | chat_id |
 1               1      

这是我的带有sql语句的PHP代码

     $chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];

$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));

    while ($row = mysqli_fetch_array($result1)) {
    $chat = $row['chat_id'];

     $sql3 ="SELECT * FROM (
                SELECT * FROM chat,chatroom_chat WHERE id = '$chat' AND chat_id= '$chat' ORDER BY id DESC LIMIT 0,40
                ) sub
                ORDER BY id ASC LMIT 0,40";

    $getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));

    $getUserID=mysqli_query($connection, "SELECT * FROM chat WHERE id = '$chat'") or die(mysqli_error($connection));

    while($getUserIDRow=mysqli_fetch_array($getUserID)){
        $postUserID=($getUserIDRow['user_id']);
    }

    $getUsername=mysqli_query($connection, "SELECT * FROM user WHERE id = '$postUserID'");
    while($getUsernameRow=mysqli_fetch_array($getUsername)){
        $postUsername=($getUsernameRow['username']);
    }

    while($row3 = mysqli_fetch_array($getChatData)) {
        $color = ($row3['user_id'] == $userID) ? '#FFFFFF' : '#66FFFF';
        $position = ($row3['user_id'] == $userID ) ? 'right' : 'left';
        $border = ($row3['user_id'] == $userID) ? ' 1px solid black ' : ' none ';

        echo "<div class='msg-dateandtime' style='text-align:$position; float:$position;'> <div class='left-username' style='color:blue;'>" .$postUsername."</div>"
                . "<div class='space'></div>"
                . "<div class='right-date'>  ". $row3['date'] ." </div></div>"
                . "<div class='wrap-message' style='background-color:$color; border:$border; float:$position;'>"
                . "<p style 'text-align=$position; margin:0; padding:0; text-align:left;'> ".$row3['chat']."</p></div>";
    }

}

现在没有任何错误显示。只是聊天不会限制在40岁。

2 个答案:

答案 0 :(得分:2)

可能您想使用连接查询。

另外,我首先想到的是while循环中的代码是你在同一个表上运行其他一些mysql查询。 将查询用作

   $sql3 ="SELECT * FROM (
SELECT * FROM chat LEFT JOIN chatroom_chat ON chat.id = chatroom_chat.chat_id WHERE id = $chat ORDER BY id DESC LIMIT 0, 40
) sub
ORDER BY id ASC";

这将使用chat.id = chatroom_chat.chat_id

从表中选择结果

发布完整的代码,以便进一步优化。 希望它有所帮助。

@Chew ..在这里你完整的代码。只需根据需要验证参数和字符串

$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];

    //This will select all the chats from table chat with id corresponding to that of chat_id in chatroom_chat table
    $sql3 ="SELECT * FROM (
            SELECT * FROM chat LEFT JOIN chatroom_chat ON chat.id = chatroom_chat.chat_id WHERE chatroom_id = $chatroomID ORDER BY id DESC LIMIT 0, 40
        ) sub 
        ORDER BY id ASC";
    $getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));

    $chatData = array();
    $user_ids = '';
    while($getRowChatData=mysqli_fetch_assoc($getChatData)){
        $chatData[]= $getRowChatData;
        $user_ids .= $getRowChatData['user_id']+',';
    }

    $users = array();
    $getUsername=mysqli_query($connection, "SELECT * FROM user WHERE id IN ($user_ids);
    while($getUsernameRow=mysqli_fetch_assoc($getUsername)){
        $users[(string)$getUsernameRow['id']]    =    $getUsernameRow['username'];
    }

   for($i = 0; $i < count($chatData); $i++) {

        $color = ($chatData['user_id'] == $userID) ? '#FFFFFF' : '#66FFFF';
        $position = ($chatData['user_id'] == $userID ) ? 'right' : 'left';
        $border = ($chatData['user_id'] == $userID) ? ' 1px solid black ' : ' none ';

        echo "<div class='msg-dateandtime' style='text-align:$position; float:$position;'> <div class='left-username' style='color:blue;'>" .$users[$chatData['user_id']]."</div>"
                . "<div class='space'></div>"
                . "<div class='right-date'>  ". $chatData['date'] ." </div></div>"
                . "<div class='wrap-message' style='background-color:$color; border:$border; float:$position;'>"
                . "<p style 'text-align=$position; margin:0; padding:0; text-align:left;'> ".$chatData['chat']."</p></div>";
    }

}

答案 1 :(得分:2)

SELECT * FROM chat INNER JOIN chatroom_chat ON
chat.id=chatroom_chat.chat_id WHERE chat.id='$chat' ORDER BY id DESC LIMIT 40;

您可以使用内部联接编写较短的查询,如上所述。它也很容易理解。您只需指定LIMIT 40即可获得40行。