Diyan salman
Diyan salman

Reputation: 45

Update mysql table value in another table

I have two tables. I want to update some value in another table

Table 1

id | value    | prefix
------------------------
1  | red      | 0
2  | blue     | 0
3  | green    | 1
4  | yellow   | 1
5  | pink     | 1
6  | black    | 1

I want output like this

Table 2

id | value   | prefix
------------------------
1  | green   | 1
2  | yellow  | 1
3  | pink    | 1
4  | black   | 1

and table 2 values already exists. I want output like this

 id   | value   | prefix
 ------------------------
  1   | book    | 0 
  2   | pen     | 0
  3   | green   | 1
  4   | yellow  | 1
  5   | pink    | 1
  6   | black   | 1

Upvotes: 0

Views: 86

Answers (2)

Ali
Ali

Reputation: 458

First you need to select data from table1 then insert into table2 Try this:

insert into table2 select value, prefix from table1 where table2.prefix = 1

Nevertheless, I don't think your approach is efficient

Upvotes: 2

Prashanth Konda
Prashanth Konda

Reputation: 33

Use SQL JOIN statements. There are various JOINs, must you learn all kinds of them so that you have a better hang on what kind of a JOIN you want to use. It's well explained at: https://www.w3schools.com/sql/sql_join.asp

Upvotes: 1

Related Questions