Reputation: 4541
I need help to write a MySQL query that would do the following for me:
It would select the last row from a certain column. Let's say the table name is 'mysite_codes' and the column name is 'code', there are many rows in this column, but I want the query to take the last added one and get my back one result in PHP (LIMIT 1 thing).
Would anyone please help me with this?
Upvotes: 1
Views: 152
Reputation: 7901
Assuming you have an auto-incremementing id column called something like "auto_id_column":
SELECT code FROM mysite_codes ORDER BY auto_id_column DESC LIMIT 0, 1;
Upvotes: 0
Reputation: 181460
Try this:
select code
from mysite_codes
order by add_date desc
limit 1
Upvotes: 0
Reputation: 385385
MySQL tables have no inherent sorting. If you want "the last added one" then you'll need an AUTO_INCREMENT
ing column like id
.
Then you can write.
SELECT `code` FROM `mysite_codes` ORDER BY `id` DESC LIMIT 1
to get just the row with the highest id
value.
Upvotes: 6