Programming with Visual Basic problem

Asked By 0 points N/A Posted on -
qa-featured

Hello,

Can you help me with this problem of mine with Visual Basic programs? 

What if I need to fetch only the last 5 records from a table with fields like ID, City, State, and Address?

How will I make this possible?

Please guide me.

Thanks.

SHARE
Answered By 0 points N/A #107465

Programming with Visual Basic problem

qa-featured

 

I think, what you want to have here is not in the Visual Basic code but about the query that should handle on how to do some action like fetching the last 5 records from your database. The solution I've got for this problem is to use "limit" command in a query.
 
How to use command:
 
Query format:
 
"SELECT * FROM table_name ORDER BY field_name DESC LIMIT 0, 10"
 
0 is the starting point of the records to be fetched and 10, the range or the number of records to be fetched.
 
Actual query: 
 
"SELECT id, state, city, address FROM table_name ORDER BY id LIMIT 5"
 
The number 5 there is the range of to be fetched record. If you directly put single digit after the 'limit" command,
 
Automatically it will designate the number inputted as the range and the starting point field will be disregarded.
 
Note: the ORDER BY and DESC (i. e Descending) are needed to fulfill the actions to be done. The ORDER BY selects the field from the table which will be used as reference in an order whether in Ascending (ASC) or Descending (DESC) order.
 
In this case, the field that must me put in the ORDER BY should be the table's ID or the primary key so that it can display much clearer records to be fetched.

Related Questions