sqlmysqlselect

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 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?


Solution

  • MySQL tables have no inherent sorting. If you want "the last added one" then you'll need an AUTO_INCREMENTing 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.