Home > Blockchain >  Get Details of all customers last inserted values
Get Details of all customers last inserted values

Time:11-05

I'm trying to get last inserted data of each customer.i am using the given query

SELECT id,customer,value from customer GROUP BY customer ORDER BY id DESC

The table i have is below

id customer Value
1 aaa 1.6
2 abc 2.7
3 aaa 8.6
4 acd 7.5
5 abc 1.6

From the above table i want the result like this:

id Name Value
3 aaa 8.6
5 abc 1.6
4 acd 7.5

CodePudding user response:

SELECT `id`, customer, `value`
  from customer c
 where c.`id` = (select max(cc.id) from customer cc where cc.`customer` = c.`customer`)

CodePudding user response:

You can use:

    SELECT id,
       customer,
       `value` 
FROM customer 
WHERE (customer,id) in 
        (
          SELECT    customer , 
                    max(`id`) as id 
          FROM customer 
          GROUP BY customer 
         ) ;
            

Demo: https://www.db-fiddle.com/f/qff694udysNgqbyJyFcDzn/5

  • Related