2016-05-29 46 views
2

目前我有3個表,我想了選擇2臺ID和限制秀達內容如何從2數據庫表中選擇並限制40中的內容?

表名作爲聊天

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

表名作爲chatroom_chat

chatroom_id | chat_id | 
1    1  

這裏是我的php代碼與sql語句

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

} 

now t這裏沒有任何錯誤顯示。只是聊天不會限制在40上。

+0

如果你在'ORDER BY id ASC'旁添加了一個額外的'LIMIT 40',那麼怎麼辦? –

+0

它會有錯誤'你的SQL語法有錯誤;檢查對應於您的MariaDB服務器版本的手冊,在第4行'LMIT 0,40'附近使用正確的語法。 – Chew

+0

確保您爲每個派生表使用別名。例如'chat.id'而不是'id'。它也將增強查詢的可讀性 – sabith

回答

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

這將選擇結果fromthe與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>"; 
    } 

} 
+0

我已經更新了整個代碼 – Chew

+0

tq非常我的想法 – Chew

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行。

相關問題