There is a table with data:

Dialog id: 12345 Dialog id: 12345 Dialog id: 12346 

Need to withdraw:

 Dialog id: 12345 Dialog id: 12346 

Here is the output code:

 $dialogs = mysql_query("SELECT * FROM konsultant"); while ($dia = mysql_fetch_array($dialogs)) { echo ' <tr> <td style="border-bottom:0px;">'.$dia['dialog_id'].'</td> <td style="border-bottom:0px;">'.$dia['client_ip'].'</td> <td style="border-bottom:0px;">'.$dia['client_msg'].'</td> </tr>'; } 

    2 answers 2

    Not very sure what kind of request

     SELECT `id`, `dialog_id`, `client_ip`, `client_msg` FROM konsultant GROUP BY dialog_id 

    will give the desired result. Therefore, if a little complicate and do so:

     SELECT k.`id`, k.`dialog_id`, k.`client_ip`, k.`client_msg` FROM konsultant k, (SELECT id FROM konsultant GROUP BY dialog_id ) AS temp WHERE k.`id` = temp.`id` 

    It should help and initially select single records. For in the subquery, we will choose so as to obtain individual records, but basically we will take this into account.

    And then in the loop we simply go over all the selected entries and do what we want.

    • It seems that GROUP BY dialog_id works. Thanks - Sauron
    • @Sauron I'm just afraid that the banal GROUP BY dialog_id group up the wrong data from the columns - Alexey Shimansky
    • I felt like the same thing. But I wanted to get the most recent entry with the dialog_id, I wonder how to do it ... - Sauron
    • GROUP BY takes an arbitrary group string. And there is no sorting. But the fact that it was necessary to take the last of all - it was necessary to clarify the question from the very beginning. Well this is an important point) - Alexey Shimansky
    • I just decided to make the Последнее сообщение от клиента: - Sauron
      $already_in = array(); // сюда будем добавлять полученные dialog_id $dialogs = mysql_query("SELECT * FROM konsultant"); while ($dia = mysql_fetch_array($dialogs)) { if(!in_array($dia['dialog_id'], $already_in)) { // если ещё не встречалось таких dialog_id $already_in[] = $dia['dialog_id']; echo ' <tr> <td style="border-bottom:0px;">'.$dia['dialog_id'].'</td> <td style="border-bottom:0px;">'.$dia['client_ip'].'</td> <td style="border-bottom:0px;">'.$dia['client_msg'].'</td> </tr>'; } }