'insert if exist on another table mysql on a specific value
I have 2 tables.
Table1 has 2 columns, id and city with 4 rows (names of city).
| id | city |
|---|---|
| 1 | surabaya |
| 2 | jakarta |
| 3 | bandung |
Table2 has 4 columns, id, city, produk and price.
| id | city | produk | price |
|---|---|---|---|
| 1 | depok | apel | 1500 |
| 2 | pekanbaru | jeruk | 2000 |
| 3 | Nasional | mangga | 2200 |
I have 1 record on table2 that has value "Nasional" on column city
How can I update this 1 row and insert the names of the city on table1
expected result
| id | city | produk | price |
|---|---|---|---|
| 1 | depok | apel | 1500 |
| 2 | pekanbaru | jeruk | 2000 |
| 3 | surabaya | mangga | 2200 |
| 4 | jakarta | mangga | 2200 |
| 5 | bandung | mangga | 2200 |
How can I achieve this
INSERT INTO table2
WHERE EXISTS (
SELECT name FROM table1 WHERE name='nasional'
);
Solution 1:[1]
I believe this is what you need:
UPDATE table2
SET name="nasional"
WHERE table2.name = table1.name;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Imanatas |
