How to select just the top row from a database

Once upon a time I needed to select just the top row from a database, this was actually so I could get the names of the columns that were defined in the table. I had my reasons, okay! Well, I’ve found a better way than doing it this way now (after all, this won’t work on an empty table), but that’s another post. So, to select the first row from an sql database, you can do either of these. The first works in MS Access, the second in PostgreSQL.

SELECT TOP 1 * FROM table;

SELECT * FROM "table" LIMIT 1;

Simple, eh? The second probably works in MySQL too, but I’ve not tried it.

MySql’s last_insert_id always returns 0

“Aaaaagh!”, I was heard to scream several times. Why won’t the damn thing return the last id. I’m doing exactly as I’ve done a million times before, yet it aways returns a zero. I don’t have to be transaction based to use last_insert_id do I?!? Well, close actually. Not in a transaction, no. But on the same database connection. See, I was issuing an insert command via the phpMyAdmin program, then trying to get the last id in code.

So:

SELECT LAST_INSERT_ID();

…does indeed work. Grrr.