Skip to content
Advertisement

How to compare SQL fields of different tables, then replace the with value other fields?

Situation: I am using wordpress and I have 60000 old posts. Now I have to update each posts with some new information. For some reason, I have to compare the custom fields in SQL and find the same post id. Then make the changes.

SQL struction:

table1(t1): wpxf_posts column name1(c1): ID column name2(c2): post_name

table2(t2): wpxf_postmeta column name3(c3): post_id column name4(c4): code

What I have to do:

  1. Compare value of c1 and c3
  2. if c1 = c3
  3. then c4 = c2

Here is a picture to help to describe

image

I read some posts but most of them teach us how to compare the difference. I am not sure if they suit my case as it seems did not find the same value. This one may be helpful but I don’t know how to apply to my situation. SQL Replace used during a compare

Advertisement

Answer

Try using a case statement:

Update table2
Set c4 = Case When c1 = c2 Then c2 Else c4 End
From table2
     Join table1
          On (What ever column you should join on)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement