Note that there are some explanatory texts on larger screens.

plurals
  1. POModifying the group by column to get the newest result on top
    primarykey
    data
    text
    <p>I am sorry if this has been already posted or its on internet. I came here after long search</p> <p>Suppose this is the table:</p> <pre><code>+----+-------+----------+---------------------+ | id | name | group_id | created_time | +----+-------+----------+---------------------+ | 1 | foo | 1 | 2010-09-22 00:00:00 | | 2 | rafi | 2 | 2010-09-23 00:00:00 | | 3 | rafi1 | 2 | 2010-09-24 00:00:00 | | 4 | rafi2 | 2 | 2010-09-25 00:00:00 | | 5 | bar | 5 | 2010-09-26 00:00:00 | | 6 | baz | 6 | 2010-09-27 00:00:00 | | 7 | baz1 | 6 | 2010-09-26 00:00:00 | | 8 | rafi3 | 2 | 2010-09-24 00:00:00 | | 9 | baz2 | 6 | 2010-09-30 00:00:00 | +----+-------+----------+---------------------+ </code></pre> <p>What I want is to group these according to group ids and order it by created_time desc(newer first)</p> <p>but when i say </p> <pre><code>SELECT id,name,group_id,created_time FROM test group by group_id ORDER BY id desc; </code></pre> <p>I get this</p> <pre><code>+----+------+----------+---------------------+ | id | name | group_id | created_time | +----+------+----------+---------------------+ | 6 | baz | 6 | 2010-09-27 00:00:00 | | 5 | bar | 5 | 2010-09-26 00:00:00 | | 2 | rafi | 2 | 2010-09-23 00:00:00 | | 1 | foo | 1 | 2010-09-22 00:00:00 | +----+------+----------+---------------------+ </code></pre> <p>what i want is to get something like this</p> <pre><code>+----+------+----------+---------------------+ | id | name | group_id | created_time | +----+------+----------+---------------------+ | 9 | baz2 | 6 | 2010-09-30 00:00:00 | | 5 | bar | 5 | 2010-09-26 00:00:00 | | 5 | rafi2| 2 | 2010-09-25 00:00:00 | | 1 | foo | 1 | 2010-09-22 00:00:00 | +----+------+----------+---------------------+ </code></pre> <p>I have tried </p> <pre><code>SELECT max(date(created_time)) as foo,name,group_id FROM test group by group_id ORDER BY foo desc; </code></pre> <p>I get the dates right but cant get the name right.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload