Deepak Mani
Deepak Mani

Reputation: 1099

Query to select newly added records only

As I am new to mysql, let me clear this doubt. how to write a query to find/select the latest added records only?

Example: Consider a Table, which is daily added certain amount of records. Now the table contain 1000 records. And the total 1000 records are taken out for some performance. After sometimes table is added 100 records. Now I would like take the remain 100 only from the 1100 to do some operation. How to do it?

(For example only, I have given the numbers, But originally I don't know the last updated count and the newly added)

Here My table contain three columns Sno, time, data. where Sno is indexed as primary key.

Sample table:

| sno | time                | data    |

|   1 | 2012-02-27 12:44:07 |     100 |

|   2 | 2012-02-27 12:44:07 |     120 |

|   3 | 2012-02-27 12:44:07 |     140 |

|   4 | 2012-02-27 12:44:07 |     160 |

|   5 | 2012-02-27 12:44:07 |     180 |


|   6 | 2012-02-27 12:44:07 |     160 |

|   7 | 2012-02-28 13:00:35 |     100 |

|   8 | 2012-03-02 15:23:25 |     160 |

Upvotes: 3

Views: 3487

Answers (2)

Naveen Kumar
Naveen Kumar

Reputation: 4601

Create table as below

Create table sample
(id int auto_increment primary key,
time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
data nvarchar(100)
);

then query as

select * from sample order by time desc limit 1

Upvotes: 0

Devart
Devart

Reputation: 122002

Add TIMESTAMP field with 'ON UPDATE CURRENT_TIMESTAMP' option, and you will be able to find last added or last edited records.

Automatic Initialization and Updating for TIMESTAMP.

Upvotes: 6

Related Questions