S
S
svilkov872016-12-23 16:42:29
MySQL
svilkov87, 2016-12-23 16:42:29

How to build a sql query?

Good afternoon!
There is a table in the database:
efd0b937657e49e4ae1f7c1920dc4085.jpg
How to write a query so that an array with the latest records (that is, the maximum id ) is returned, but the values ​​​​in the uid column are not repeated.
At the output you need something like this:

array(2) {
  [0]=>
  array(2) {
    ["uid"]=>
    string(2) "10"    
   ["text"]=>
    string(2) "айяйяй"
  }
  [1]=>
  array(2) {
    ["uid"]=>
    string(2) "11"    
   ["text"]=>
    string(2) "бебебе"
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ramm, 2016-12-23
@rammtw

select * from table group by uid order by id desc limit 2

R
Rsa97, 2016-12-23
@Rsa97

Choose the maximum id grouped by uid and join the table by id to this selection

D
Dmitry Entelis, 2016-12-23
@DmitriyEntelis

select uid, text from (select uid, max(id) from `table` group by uid) tmp 
join `table` on tmp.uid = table.uid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question