I apologize in advance if this was asked already, I tried searching but couldn't find an exact match.
Basically, I have a table that contains themes. Use the snippet below as a rough idea of how the table is set up.
theme_name field_name color
'Default' 'Header' 'Orange'
'Default' 'Footer' 'Orange'
'Default' 'Body' 'Orange'
'Newtheme 'Header' 'Red'
'Newtheme' 'Footer' 'White'
'Newtheme' 'Body' 'Blue'
Basically, what I need to do is write a query that updates the values in the "color" field for each row of theme_name default to match those of the corresponding values on the rows for Newtheme. In other words, it should look like this at the end:
theme_name field_name color
'Default' 'Header' 'Red'
'Default' 'Footer' 'White'
'Default' 'Body' 'Blue'
'Newtheme 'Header' 'Red'
'Newtheme' 'Footer' 'White'
'Newtheme' 'Body' 'Blue'
Unfortunately, there is no unified ID number on the table for every row with the same theme_name, and I am unable to create one.
Now, each theme has a lot more rows in the real table than the example I gave. I could write use
SET value=(Select value
from Table
where theme_Name='Newtheme')
where theme_name='Default'
and field_name='Header'`
That would fix a single row. Then I could repeat that update statement for each and every row. However, I suspect there's a much quicker way to do this, where I could update all of them in one single update statement. I just have no idea what it is.
Hopefully, my request makes sense. Thank you in advance.
Looks like a simple co-related UPDATE query:
update the_table
set color = (select color
from the_table t2
where t2.theme_name = 'NewTheme'
and t2.field_name = the_table.field_name)
where theme_name = 'Default';
This would fail if (theme_name, field_name)
is not unique however
You can use self-join to do this. Try the following:
update a
set a.color = b.color
from themes a
join themes b on a.field_name = b.field_name and a.them_name <> b.theme_name
where a.theme_name = 'Default'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.