View Full Version : Need to check for a column in a table
TalkVirginia
03-06-2010, 08:06 AM
I'm doing an addon and I an install plugin I need to do the following in a mySQL query or a series of queries.
Psuedocode:
add a new column
check for an existing column from an old version of the addon.
If the old column exists
Move the data for all records to the new column
Anyone know if this is possible and if so how would I go about it?
Dylanblitz
03-06-2010, 06:24 PM
Haven't tested it but this should work, just put it in your product xml install code and replace with the real variables.
$db->query_write("ALTER TABLE " . TABLE_PREFIX . "TABLE_NAME ADD NEW_COLUMN mediumint(8) unsigned NOT NULL DEFAULT '0' AFTER AN_EXISTING_COLUMN");
$column_query = $db->query_read("SHOW COLUMNS FROM " . TABLE_PREFIX . "TABLE_NAME LIKE 'OLD_COLUMN_NAME'");
while ($column_data = $db->fetch_array($column_query))
{
$db->query_write("UPDATE " . TABLE_PREFIX . "TABLE_NAME SET NEW_COLUMN = OLD_COLUMN_NAME");
}
TalkVirginia
03-06-2010, 06:40 PM
Thanks! This looks a bit easier than what I was originally toying with. I was trying to do it with a table join to itself in an update query.
Something like:
UPDATE TABLE_NAME as T2
SET t2.NEW_COLUMN = t1.OLD_COLUMN
INNER JOIN TABLE_NAME T1
ON T2.UserID = T1.UserID
WHERE T2.UserID = @UserID
I just wasn't able to figure out how to loop it to get the userid.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.