-1

I'm trying to make this query to look for the records whose id are from a certain customer, but it returns the first records of the table. I need to do this same query, but that returns the last records of the table, that is, the most current ones. Any idea how I can do this?

SELECT table1.sl,table1.idm 
FROM table1 
INNER JOIN table2 ON table1.idm=table2.idm 
WHERE table2.idc=4 
GROUP BY table1.idm

This query returns the first records of the table.

edit* I managed to accomplish what I need using the following query, however, it took 20 seconds to execute.

SELECT table1.sl, table1.idm FROM table1 INNER JOIN table2 ON table2.idm = table1.idm WHERE table2.idc=132 AND table1.id IN ( SELECT MAX(id) FROM table1 GROUP BY idm ) GROUP BY table1.idm

1 Answers1

-1

There is no sample data to understand what is happening. If you can share that it will be more clear.

Few things you want to do,

  1. Define the order of the record. You need to sort records based on some fields to have predictable output. Refer - https://dev.mysql.com/doc/refman/8.0/en/sorting-rows.html
  2. You can sort ascending or descending way to get the top/bottom result.
  3. If you need nth record , you can refer - Mysql retriving the nth record
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47