2

I have used the following query to insert values in table

INSERT INTO `tbl1` SELECT * FROM tbl2

Over here tb1 is a temp table.

Now, I want something like this

UPDATE `my_table` SELECT * FROM tbl1

I know that the syntax for update is Update tbl SET cols = vals But can we have something like the insert query above ?

Thanks.

1
  • don't you think so that you have given insufficient data Commented May 21, 2012 at 6:49

3 Answers 3

4

You can doInsert with Select but not Update with Select. But still possible by using JOIN within UPDATE.

UPDATE table1 t1 JOIN table2 t2 ON t1.id = t2.id
SET t1.col1 = t2.col2, t1.col2 = t2.col2
Sign up to request clarification or add additional context in comments.

2 Comments

I think you mean SET t1.col1 = t2.col1
Thanks @Rahul but it's only just an example. I was trying to illustrate the format of join not the data assignment because what if col1 and col2 from t1 should have the value of col2 from t2?
3

You can join your tbl1 table with my_table using the multiple-table UPDATE syntax:

UPDATE my_table JOIN tbl1 ON ***join_condition***
  SET my_table.foo = tbl1.bar, ...

Comments

1

You can do something like this:

update my_table join tbl1 on my_table.id = tbl1.id
set my_table.Vaal= tbl1.vaal 

1 Comment

Yaa, that's actually SQL Server syntax, not mysql. though they are almost same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.