MySql Error #1054 when adding new column to table using prepared statement I'm trying to add a new column named lot to a table, but I'm getting error #1054 - Unknown column 'lot' in 'field list'. What could be the problem? Here's the query:

SET @lot = (SELECT lot_no_start FROM `systemconstants`);
SET @sql = CONCAT('ALTER TABLE ipp_sorted ADD `',lot,'` INT PRIMARY KEY AUTO_INCREMENT=',@lot);
PREPARE st FROM @sql;
EXECUTE st;
Remember that building SQL in this way is just building a String that happens to contain some text that your DBMS understands. 

Your new column name, >>lot&lt;&lt;, is just part of that SQL: 

SET @sql = CONCAT( 'ALTER TABLE ipp_sorted ADD `lot` INT PRIMARY KEY AUTO_INCREMENT=', @lot );


Leaving >>lot&lt;&lt; outside the string literal, the DBMS is trying to interpret it as a column in something or other (variables being prefixed with '@' signs) but it can't find such a column, so gives up with this error.