-2

I have to get the last 50 records from my MySQL database.

Here is the structure of my test database:

ID S1 S2 S3 Date-time Label
13 32 55 33 2017-09-05 13:15:06 temperature
16 111 222 66 2017-09-05 19:22:14 temperature
17 44 55 33 2017-09-05 19:22:14 temperature
18 55 11 88 2017-09-12 14:22:00 temperature
21 77 1 200 2017-09-15 12:24:06 temperature
22 22 55 11 2017-09-19 14:37:00 temperature

How could I show only the last 3 data? for example:

18 55 11 88 2017-09-12 14:22:00 temperature
21 77 1 200 2017-09-15 12:24:06 temperature
22 22 55 11 2017-09-19 14:37:00 temperature

Greetings and thank you.

Manolait
  • 353
  • 2
  • 11
  • Show us the query that you tried. – takendarkk Sep 20 '17 at 13:15
  • 1
    Possible duplicate of [How do I do top 1 in Oracle?](https://stackoverflow.com/questions/3451534/how-do-i-do-top-1-in-oracle) – JNevill Sep 20 '17 at 13:18
  • Hello, I have tried a very simple query but the top does not recognize Oracle SELECT TOP 5 * FROM iof.inv; ERROR: 15:18:32 SELECT top 5 * FROM iof.inv Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5 * FROM iof.inv' at line 1 0.000 sec – Manolait Sep 20 '17 at 13:21
  • The last 3 based on what? the id, the date? – Cyrille MODIANO Sep 20 '17 at 13:32
  • I'm probadoe sta query SELECT * FROM iof.inv where id <= 50 is there any way to know the value of the last id that I have and that collects 50 before the last one? @csm_dev – Manolait Sep 20 '17 at 13:32
  • You say you are using oracle but the error message says you are using MySQL. Which is it? – takendarkk Sep 20 '17 at 13:33

4 Answers4

0

FOR ORACLE:

   SELECT * FROM (
       SELECT ID,
              S1,
              S2,
              S3,
              Date-time,
              Label
        FROM TABLE
        ORDER BY ID DESC)
    WHERE ROWNUM <= 50;

FOR MYSQL:

SELECT ID,
      S1,
      S2,
      S3,
      Date-time,
      Label
FROM TABLE
ORDER BY ID DESC
LIMIT 50; 

Here is a quick doc:

https://www.w3schools.com/sql/sql_top.asp

Edit:

For the last 50 rows:

SELECT * FROM (
    SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC
Cyrille MODIANO
  • 2,246
  • 2
  • 21
  • 33
  • hi First of all I apologize I'm using MYSQL and not ORACLE. now proceed to change the name of the post. If it is preferable delete the post and create another. I am using this sentence: SELECT * FROM iof.inv ORDER BY id DESC LIMIT 3 But as I said it is not valid since I retrieve the table in JSON and construct a graph with this data and in this way would have the graph alreves, the last the first. Is there a way to do it without having to sort the ID? Thank you – Manolait Sep 20 '17 at 16:12
0

In Oracle12c you can use the fetch keywork:

SELECT *
FROM   table 
ORDER BY id DESC
FETCH FIRST 50 ROWS ONLY;
Renato Afonso
  • 654
  • 6
  • 13
  • hi First of all I apologize I'm using MYSQL and not ORACLE. now proceed to change the name of the post. If it is preferable delete the post and create another. I am using this sentence: SELECT * FROM iof.inv ORDER BY id DESC LIMIT 3 But as I said it is not valid since I retrieve the table in JSON and construct a graph with this data and in this way would have the graph alreves, the last the first. Is there a way to do it without having to sort the ID? Thank you – Manolait Sep 20 '17 at 16:12
0

Use Top N Query (row num<=50) fro first, for last 50 you can use "order by id desc"

  • Please be more detailed in your answer and make sure that it has not already been answered. See more here: https://stackoverflow.com/help/how-to-answer – pucky124 Sep 20 '17 at 19:23
0

First I was confused with the Post between ORACLE and MYSQL I apologize.

The solution at the end was the following:

SELECT * FROM inv ORDER BY id DESC LIMIT 50

then transform the ARRAY that I collect with the function:

var dorde = d0.reverse ();

thanks for everything.

Manolait
  • 353
  • 2
  • 11