1

If i have a table

parent_table {ID,code} 

2 colums. ID - primary key

childe_table {ID,parent_table_ID, Name} 

3 columns. parent_table_id is foreign key

I'm trying to create query to do this:

if(parent_table.code == 'x'){
    child_table.Name == 'value'
}

I know that I should use joins to do this. Could someone show me an example?

Evgeny
  • 3,910
  • 2
  • 20
  • 37
Lauris01
  • 283
  • 1
  • 4
  • 19

2 Answers2

2

Look at this answer. You need to do

 UPDATE childe_table ct 
   JOIN parent_table pt ON ct.parent_table_id = pt.id  
   SET ct.code="value"
   WHERE pt.code='x'
Community
  • 1
  • 1
0

Try this

update childe_table c 
set c.Name="value" 
where (select p.code from parent_table p where p.id=c.parent_table_id)="x"
Evgeny
  • 3,910
  • 2
  • 20
  • 37
  • This is a very inefficient way of doing an update across a join. Instead of doing the join in the update, you are doing it on an inner select statement that gets executed for every record in the child table. –  Jan 08 '16 at 14:40