Good day.

There is a label, fields: id, ttl, str (~ 100 records). How to ttl , and select unique entries among them? Can all these records be written to an array?

    3 answers 3

    Frankly, I do not understand why already 3 plus received a request

      select `ttl` from `table` group by `ttl` order by `id` 

    All of you in real codes write such nonsense instead of simple DISTINCT?

    2 @ Yoharny Babai - there is no need to add AS __ttl , mysql will return an answer with fields that will be called ttl .

    And that:

      $dbResult = mysql_query('SELECT DISTINCT ttl FROM table'); $ttlData = array(); // массив для заполнения значениями while($row = mysql_fetch_assoc($dbResult)) { $ttlData[] = $row; // собственно заполнение } // в данном случае получим массив вида // -> array( array('ttl'=>'...'), array('ttl'=>'...'), array('ttl'=>'...') ... ) // этот способ описан для того чтобы у вас больше не возникал вопрос о том как // сложить выборку БД в массив 

    If you need to get an array of the form

      // array('...', '...', '...', ...); 

    Then

      $ttlData = array(); // массив для заполнения значениями while($row = mysql_fetch_assoc($dbResult)) { $ttlData[] = $row['ttl']; // собственно заполнение } // на выход - массив значений 

    PS: @Shrek it's great that you know that DISTINCT performs an implicit GROUP BY, but instead of writing DISTINCT, "bullshit".

    • So, there is not much difference, I can add and answer DISTINCT, not a question. Everyone uses different functions differently. - Artem
    • So there is a difference, the difference is not in performance but in the fact that my request is clear and normal, the same as you wrote is nonsense. What you write is read by people, and in this case they will also be doing as you wrote (if newbies understand it) because The answer received many votes (most likely from them). - Zowie
    • But it is not everywhere to push DISTINCT only because of the fact that he, as you say, is understandable and normal. The syntax of your and my queries is clear, so this is the same MySql syntax. He never change. Wrote 2 requests and it is not always necessary to use DISTINCT. it also depends on the situation. - Artem
    • You can also use SELECT * FROM table WHERE id = '' OR id = '' OR id = '' OR id = '' Instead of SELECT * FROM table WHERE id IN ('', '', ''); Both this and that sql, but the difference, as for me, is absolutely obvious, but you would probably say here - in fact, IN('','','') is the same as id='' OR id='' OR id='' OR id='' , why "complicate" the request (frankly, even sarcasm did not work here, because I don’t understand how such a thing could be written in the answer to such a question) that the option with group by .. order by .. absolutely redundant - Zowie
    • no need to add AS __ttl I'll know ... - Yoharny Babay
     $query="select DISTINCT(ttl) FROM `table`" // первый вариант Юзайте этот (говорят он понятнее для Noob) $query="select `ttl` from `table` group by `ttl` order by `id`" // второй вариант $result=mysql_query($query); 

    In fact, you will choose everything, you will group them. they will be unique and then in php

     while ($row=mysql_fetch_array()) { echo "Ttl=".$row['ttl']."<br />"; } 

    DISTINCT, as described, performs implicit GROUP BY . The difference in performance may be , but to explain it rationally difficult. It uses indices, but we need the correct composite indexes, since GROUP BY is executed after WHERE.

    Request time

     (7,723 total, Query took 0.0007 sec)// DISTINCT (7,723 total, Query took 0.0008 sec)// group by среднее значение примерно 0.0011 sec 
    • amazing, but only I didn’t want them to do "echo", I wanted an array with unique records, and only then to be odd with them 8-) - frank
    • one
      so process it as you want, I just wrote an example of the output. feeling that I just impose my code and only as it was written by the author, without the right to edit :) - Artem
    • That's all. As if the discussion on the topic that I won badly. It’s not a shame to me, it’s just that the author of the question is in a mess in the head; I repeat it does not hurt. I can even delete the answer - Artem
    • @Shrek - this is what is written? O_o - Zowie
    • to the very same. - Artem
     SELECT DISTINCT(ttl) AS _ttl FROM `table` ORDER BY _ttl