Jump to content
xisto Community
jedipi

[mysql]how Can I Get The Newest Record?

Recommended Posts

I am currently developing a website which need to show the newest record in index page.The newest record should retrieve from mysql database.now, the question is....how can i get the newest inserted record from a particular table in myssql database???

Share this post


Link to post
Share on other sites

Dear jedipi,

 

 

While deciding on the columns (=fields) of your data-base table,

you should definitively use an extra column that has the

data-base attribute autoincrement .

 

This extra column will be used by the data-base engine as a counter,

and the value of this column will be created by the data-base engine,

everytime that a new row is inserted into the data-base table.

 

Its value will auto-increment - as the name suggests.

 

Most data-base designers use the name id for this extra column.

 

If you want to obtain the last inserted row of the data-base table,

all you have to to is request the row that has the highest value in that column.

 

You ask the highest value with the MAX() data-base function, as in

SELECT MAX(id) FROM myuserid_mydatabasetable;

Share this post


Link to post
Share on other sites

Dear jedipi,

While deciding on the columns (=fields) of your data-base table,

you should definitively use an extra column that has the

data-base attribute autoincrement .

 

This extra column will be used by the data-base engine as a counter,

and the value of this column will be created by the data-base engine,

everytime that a new row is inserted into the data-base table.

 

Its value will auto-increment - as the name suggests.

 

Most data-base designers use the name id for this extra column.

 

If you want to obtain the last inserted row of the data-base table,

all you have to to is request the row that has the highest value in that column.

 

You ask the highest value with the MAX() data-base function, as in

SELECT MAX(id) FROM myuserid_mydatabasetable;

<{POST_SNAPBACK}>

Thanks signatureimage,

yea, autoincrement is a good way to do that...

I have used this method for the new tables in the database.

 

However, the system I am developping is going to be integrated with the old system. There are some tables in the database already.

Can i add extra autoincrement column to the old table without affecting the old system???

Share this post


Link to post
Share on other sites

Dear jedipi,

 

Yes, you can add a new column to an existing data-base table.

 

First method: with the interactive phpMySql dialogs that you have on your control panel of asatahost.com

 

Second method: with the SQL statement:

ALTER TABLE ...

 

May I suggest that you visit the mySQL website for the full explanation of this method.

 

There, you will also find examples of how to let the data-base engine automatically generate the missing autoincrement values.

 

The mySQL website is not the only source of valuable information, you should also visit SQL org.

There you can find more general information and tutorials.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.