how to show the last entry on first in php? [closed]
I have a program that deals with huge amount of data. The process is like this, first I enter the client's name and account number, then it's saved in a db, then I get the record from there and enter his/her electricity consumption for every month in an accounting year. I want to show the last entered data on first, so that I can apply pagination. How can I do that? Thanks for the help!
You can just call your "last data" by querying by your table Autoincrement key (usually Id) ordering by the same key (DESC) and limiting the result to fetch just the first row
Ex:
SELECT * FROM myTable ORDER BY id DESC LIMIT 1
use ORDER BY fieldName DESC
clause:
SELECT * FROM mytable ORDER BY id DESC
;
this will select records ordered by Id in descending order... you can put any column instead of id. e.g date field if it's more relative to your problem.
Assuming your DB structure is like:
Table: clients
client_id [PK]
name
...
Table: electricity_consumptions
client_id [FK]
quantity
dated
You can have an SQL like:
select * from electricity_consumptions where client_id =1 ORDER BY dated DESC
精彩评论