S
S
svilkov872016-12-23 22:55:32
MySQL
svilkov87, 2016-12-23 22:55:32

How to make such query with GROUP BY?

Good afternoon!
There is a table in the db:
d3ecad40cb5d46dd9645d61197aab955.jpg
and a query using GROUP BY:

SELECT id FROM `table` GROUP BY value ORDER BY id DESC

we get:
array(2) {
  [0]=>
  array(1) {
    ["id"]=>
    string(1) "1"
  }
  [1]=>
  array(1) {
    ["id"]=>
    string(1) "3"
  }
}

The question is how to make the result return like this:
array(2) {
  [0]=>
  array(1) {
    ["id"]=>
    string(1) "5"
  }
  [1]=>
  array(1) {
    ["id"]=>
    string(1) "4"
  }
}

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2016-12-23
@svilkov87

SELECT MAX(id) FROM `table` GROUP BY value ORDER BY id ASC

A
Alexander Aksentiev, 2016-12-23
@Sanasol

SELECT id FROM (select id, value from `table` ORDER BY id desc) as tbl GROUP BY value

ps if you have something in the ID code attached in this way and it will break from the wrong one, then you probably shouldn't do it. Because the grouping will change at any moment and the IDs there will be different and generally any. In general, it is necessary to somehow think over this more correctly.
pps Option Max is more correct, I forgot this option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question