njlqay njlqay - 3 months ago 12
MySQL Question

How to get last column name from mysql table?

I want to retrieve the last column name from a mysql table.

For example the schema would look like this:


TABLE example {surname,firstname,birthdate}


In this example I want to get the column name "birthdate" from table "example".
How do I achieve this in MySQL?

Answer

Please try this:

SELECT 
COLUMN_NAME,
ORDINAL_POSITION
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND TABLE_NAME ='YOUR_TABLE_NAME'
ORDER BY ORDINAL_POSITION DESC 
LIMIT 1;

Information_schema.columns stores column specific information.