siabanie Posted August 3, 2011 Share Posted August 3, 2011 Hi all, I would like to ask if anyone could help me about updating table. I have a table name: Property. And have 5 fields: ID, Branch, Cost, Type, Add. In the Branch field I have 5 different values: A, B, C, D, E Now I like to move ALL the data that I have in A to D and delete the A. I do not want to lost all the data I have in A nor in D - I just like to move A to D. Is mySql query here correct? mysql query UPDATE table SET branch='D' WHERE branch='A'; Can anyone please assist me? Thanks in advance..! Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/ Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 You meen you havn't even tried it? change table to property and it looks like it might. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251187 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 I have tried them but I got an error MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table SET branch='ゴルダーズグリーン' WHERE branch='フィンムthe "フィンãƒ" bit cannot be read as it's in different character (Japanese rather, the A, B, C, D, E are just an example) Any idea what mistake I did here? Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251190 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 What character encoding have you set in your database? It will need to be something that accepts multi lingual characters as input Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251201 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 What character encoding have you set in your database? It will need to be something that accepts multi lingual characters as input I have it set on utf8_general_ci - I think that is the mutlilingual char already? Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251202 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 wait a minute - are you actualy trying to update a table called "table"?!? because that is a reserved word, you should call it something else. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251206 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 wait a minute - are you actualy trying to update a table called "table"?!? because that is a reserved word, you should call it something else. Oh yes silly me!! you were right Muddy_Funster. I just run and it seems work now thanks. Is that mean that all the data in A now been has been placed in D and rename it from A to D as well? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251209 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 Not exactly like that, no. All that happend was you told the database that "FOR every record in 'table' that has an 'A' in the branch Field change this 'A' to be a 'D'" So it hasn't moved or copied any records, only changed the information that that was stored in the branch column for every one that had an 'A' to begin with. If you now run SELECT COUNT(*) AS branchCount FROM table WHERE branch = 'A' You should get 0 back as the result. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251218 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 Not exactly like that, no. All that happend was you told the database that "FOR every record in 'table' that has an 'A' in the branch Field change this 'A' to be a 'D'" So it hasn't moved or copied any records, only changed the information that that was stored in the branch column for every one that had an 'A' to begin with. If you now run SELECT COUNT(*) AS branchCount FROM table WHERE branch = 'A' You should get 0 back as the result. I see yes I got no record when I run that query - thanks. So that means every time there is a new record in A I will have to run this UPDATE query then? - Can I just move all the records in A into D, isn't much better instead of having to update it every time there is a new record found in A? What you think? Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251220 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 Best approach would be to alter the insert statement if you can, so that no records will be added to 'A'. Other than that, there is no other way that I know of than to run the update periodicaly. You don't "move" records, you only change the data that the records hold. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251252 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 Best approach would be to alter the insert statement if you can, so that no records will be added to 'A'. Ok thanks!... What do you mean by that? Can you show me an example please. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251253 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 I don't know how you are inserting your records, without knowing that I can't realy help much. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251269 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 I don't know how you are inserting your records, without knowing that I can't realy help much. Hmmm I insert the records using PHP script something like this e.g: .. $sql_query = "INSERT INTO ".TABLE_PRO." (`branch` ,`address``cost` ,`type`) VALUES ( '$branch','$address','$cost','$type','')"; .. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251274 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 then you could simply catch it in the PHP before it gets to the insert SQL: if($branch == 'A') {$branch = 'D';} Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251291 Share on other sites More sharing options...
fenway Posted August 3, 2011 Share Posted August 3, 2011 Or with a CASE statement. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251319 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 Great ideas guys, thanks a lot! Will try and working on it! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251325 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 Or with a CASE statement. Yip, it's a little more complicated though: INSERT INTO table ( `branch` ,`address``cost` ,`type` ) VALUES ( CASE WHEN '$branch' = 'A' THEN SELECT 'D' ELSE SELECT '$branch' END,'$address','$cost','$type','' ) Is how I think it would look, but I'm not sure about the use of SELECT... Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251331 Share on other sites More sharing options...
siabanie Posted August 3, 2011 Author Share Posted August 3, 2011 Or with a CASE statement. Yip, it's a little more complicated though: INSERT INTO table ( `branch` ,`address``cost` ,`type` ) VALUES ( CASE WHEN '$branch' = 'A' THEN SELECT 'D' ELSE SELECT '$branch' END,'$address','$cost','$type','' ) Is how I think it would look, but I'm not sure about the use of SELECT... Thanks...but yes quite tricky - the thing is I use array for my branch e;g branch[] So perhaps I can avoid these? Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251337 Share on other sites More sharing options...
fenway Posted August 3, 2011 Share Posted August 3, 2011 You'd have to case each field separately. Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251341 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 So what are your actual variables? are they single value ($branch), single array per SQL variable ($branch[]) or all stored in a single array? ($insert[branch, address, cost, type])? Quote Link to comment https://forums.phpfreaks.com/topic/243692-how-to-update-field/#findComment-1251480 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.