aeroswat Posted June 4, 2010 Share Posted June 4, 2010 UPDATE table SET field = replace(field, ';', ':') WHERE field LIKE '%;%'; Just curious if this would be the correct syntax to replace all my semi-colons in my 'field' column and also is there a way where I could replace ALL semi-colons in ALL columns with colons. With one query that is Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/ Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 I think that's correct, but that is completely dependent on your SQL type. You'd have better luck posting in the appropriate SQL board, not in the generic PHP board, especially since this is only a SQL question. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067801 Share on other sites More sharing options...
aeroswat Posted June 4, 2010 Author Share Posted June 4, 2010 I think that's correct, but that is completely dependent on your SQL type. You'd have better luck posting in the appropriate SQL board, not in the generic PHP board, especially since this is only a SQL question. Thank you. Ya I thought about that also but I mean I know there is always a php way of accomplishing this and perhaps if somebody had recommended a way to do it in PHP that was faster I could just go ahead and throw up a quick page to run a loop that would take care of this. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067804 Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 Well, you could do it with PHP, but it would be significantly more complicated and slower. The reason for this is that you would have to extract the database data first, then run the update query. Plus, you would have to run as many update queries as there were rows. For instance, if you had 1,000 rows, you would have 1,001 queries; one select query to get the data and 1,000 update queries to update the rows. With your original solution, just one query is required, regardless of how many rows there are. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067805 Share on other sites More sharing options...
Maq Posted June 4, 2010 Share Posted June 4, 2010 This should definitely be done in SQL. I'm moving to the MySQL section unless you specify otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067831 Share on other sites More sharing options...
aeroswat Posted June 4, 2010 Author Share Posted June 4, 2010 Well, you could do it with PHP, but it would be significantly more complicated and slower. The reason for this is that you would have to extract the database data first, then run the update query. Plus, you would have to run as many update queries as there were rows. For instance, if you had 1,000 rows, you would have 1,001 queries; one select query to get the data and 1,000 update queries to update the rows. With your original solution, just one query is required, regardless of how many rows there are. well i mean i figure i could just run a query that pulls the field names if this is possible? And i could just have a loop that runs one for each field name. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067839 Share on other sites More sharing options...
Maq Posted June 4, 2010 Share Posted June 4, 2010 Well, you could do it with PHP, but it would be significantly more complicated and slower. The reason for this is that you would have to extract the database data first, then run the update query. Plus, you would have to run as many update queries as there were rows. For instance, if you had 1,000 rows, you would have 1,001 queries; one select query to get the data and 1,000 update queries to update the rows. With your original solution, just one query is required, regardless of how many rows there are. well i mean i figure i could just run a query that pulls the field names if this is possible? And i could just have a loop that runs one for each field name. Like F1Fan said, you would need a query to grab all of the rows then queries for as many rows you have to update them all. Very inefficient. Have you tried your original query? Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067843 Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 Yes, that's basically what I said. Run a select to get the data, then loop through all the results and do an update for each row accordingly. But I don't suggest you do this. It will take significantly longer than just doing it in SQL. Why would you want to use PHP rather than SQL? Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067848 Share on other sites More sharing options...
aeroswat Posted June 4, 2010 Author Share Posted June 4, 2010 Yes, that's basically what I said. Run a select to get the data, then loop through all the results and do an update for each row accordingly. But I don't suggest you do this. It will take significantly longer than just doing it in SQL. Why would you want to use PHP rather than SQL? No no i mean run a for loop on the column names then for each do this query UPDATE table SET field = replace(field, ';', ':') WHERE field LIKE '%;%'; so if i had 20 columns thats just 20 queries Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067877 Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 Oh, you want to to that for every column? Then, yes, I suppose that would work. There may still be a way to do that in SQL. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067883 Share on other sites More sharing options...
aeroswat Posted June 4, 2010 Author Share Posted June 4, 2010 Oh, you want to to that for every column? Then, yes, I suppose that would work. There may still be a way to do that in SQL. Hoping someone might be able to enlighten me on that SQL way Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1067893 Share on other sites More sharing options...
fenway Posted June 5, 2010 Share Posted June 5, 2010 No without stored procedures there isn't. Quote Link to comment https://forums.phpfreaks.com/topic/203875-update-replace-query/#findComment-1068422 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.