I have two tables that are identical in structure. Table1
holds moderated data, table2
holds the rest.
Table 1
+------+--------+---------------+--------+-----------+ | "id" | "name" | "description" | "type" | "country" | +------+--------+---------------+--------+-----------+ | "1" | "a" | "x" | "1" | "US" | | "2" | "b" | "x" | "1" | "UK" | +------+--------+---------------+--------+-----------+
Table 2
+------+-----------+-----------------+--------+-----------+----------+ | "id" | "name" | "description" | "type" | "country" | "status" | +------+-----------+-----------------+--------+-----------+----------+ | "1" | "Title 1" | "Description 1" | "1" | "US" | "0" | | "2" | "Title 2" | "Description 2" | "10" | "UK" | "0" | +------+-----------+-----------------+--------+-----------+----------+
I run the below sql in order to update table 1
with data from table 2
, and it works well. The only problem is, I need to specify the id
in both places. If I were to specify it only in one place, where would it go?
UPDATE table1 dest,
(SELECT name,
description
FROM table2
WHERE id = 1) src
SET dest.name = src.name,
dest.description = src.description
WHERE dest.id = 1;
The way this thing goes is:
UPDATE table1 SET name AND description =
(
SELECT name, description from table2
WHERE id=1 AND country=us and type=10
) WHERE id=idfromselect AND country=countryfromselect AND type=typefromselect
I can't figure out where to put the id
and remaining conditions
. Can you help?