PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 9, 2022

[FIXED] How to write an UPDATE depending on values in other table

 December 09, 2022     join, mysql, sql, sql-update, syntax     No comments   

Issue

I have a question concerning "UPDATE" in MySQL. I have two tables table1 and table2 which are connected table1.id=table2.tab1. Now, I'd like to update a value in table2 depending on a value in table1. I was trying to do it like this:

UPDATE table2 SET table2.val='new value' 
WHERE table1.id=table2.tab1 AND table1.val='xy' 

In other words, I would like to change the value val for all entries in table2 where the corresponding entry in table1 has the value 'xy'.

Unfortunately, this doesn't work. Can someone give me a hint?


Solution

This isn't the correct syntax. You should use an update-join

UPDATE table2
JOIN   table1 ON table1.id = table2.tab1
SET    table2.val = 'new value' 
WHERE  table1.val = 'xy' 


Answered By - Mureinik
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing