lorddemos90 Posted May 8, 2007 Share Posted May 8, 2007 Is there a way to exclude one specific column in a mysql database from a command. IE: I want to use the command $sql = "TRUNCATE TABLE DailyDataSF_copy"; But have it affect all columns except just one specified column that'll stay the same. Any solutions to this? The table has like 90+ columns, which is why I'd like to do it this way if at all possible. Quote Link to comment https://forums.phpfreaks.com/topic/50537-exclude-a-column-from-a-mysql-command/ Share on other sites More sharing options...
utexas_pjm Posted May 8, 2007 Share Posted May 8, 2007 Truncating a table drops all rows. It has nothing to do with altering the table structure. Are you trying to remove the columns from the table or are you trying to "unset" the data in the columns? Quote Link to comment https://forums.phpfreaks.com/topic/50537-exclude-a-column-from-a-mysql-command/#findComment-248341 Share on other sites More sharing options...
lorddemos90 Posted May 8, 2007 Author Share Posted May 8, 2007 I want to just delete all the data from the table except for one column. Quote Link to comment https://forums.phpfreaks.com/topic/50537-exclude-a-column-from-a-mysql-command/#findComment-248344 Share on other sites More sharing options...
utexas_pjm Posted May 8, 2007 Share Posted May 8, 2007 Then you want syntax like this: UPDATE `your_table` SET `col_a` = NULL, `col_b` = 0... etc.. Some columns "erased" state are null while others are zero so you'll have to specify each one individually. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/50537-exclude-a-column-from-a-mysql-command/#findComment-248348 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 One possible approach is do a describe on the table and from that data returned loop through it and do an update on each column name or use that to build a query on the fly. That way you do not have to type in the columns. Not sure how it would work or if it would work but it might be something similiar to this: <?php $res = mysql_query("describe table_name;"); while ($row = mysql_fetch_assoc($res)) { if ($row['column_name'] != "90thcolumnname") $update .= "`".$row['column_name']."` = NULL,"; } $update = "Update `table_name` SET " . substr($update, -1); mysql_query($update); ?> Note I am not sure of the "column_name" portion or if the describe will return a result array. But something like that should work. Quote Link to comment https://forums.phpfreaks.com/topic/50537-exclude-a-column-from-a-mysql-command/#findComment-248356 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.