Asked By
peirce
0 points
N/A
Posted on - 11/08/2011
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.
Answered By
johann
0 points
N/A
#107465
Programming with Visual Basic problem
Â
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.