Jump to content

How to UPDATE field.


siabanie

Recommended Posts

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..!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? :D

Link to comment
Share on other sites

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','')";

..

 

:D

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.