I need help with a MySQL SELECT Query
I need help to write a MySQL query that would do the following for me:
It would select the last row from a certain colu开发者_如何转开发mn. 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?
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.
Try this:
select code
from mysite_codes
order by add_date desc
limit 1
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;
精彩评论