Jump to content

Changing the data after comparison


prophecym

Recommended Posts

Hi everyone,

 

I am sure there's a way to do this. I just don't know how. Any help is appreciated.

 

I have 2 tables that need to be compared based on their one particular field. Say this is the 'name' field. During the comparison of  these two tables, it needs to find the same records. Then, change the data in the another field to something else.

 

Table A                                        Table B

id | name | boolean                      name

 

As you can see above, the name field needs to be compared. When it finds the same records, the 'boolean' field in the Table A needs to be changed whatever I set to. (If it is 1, change it to 2)

 

I would love to know how to do this. Thank you.

Link to comment
Share on other sites

What you need is an UPDATE statement, with a WHERE clause to limit the rows updated to what you want.

Something like this should work, though it might require you to join table B so try it on some test data first. ;)

UPDATE `table_a` AS a SET a.`boolean` = $Val WHERE `table_b`.`name` = a.`name`

 

Another way of doing it is by using WHERE IN() and a subquery. A quick google search will give you lots of examples on how this is done.

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.