Kopiranje iz ene MySQL tabele v drugo s pogojem

Želim skopirati del podatkov iz ene MySQL tabele v drugo, rad bi dodal pogoj, v kateremu določim id-je vrstic v obeh tabelah, ki ju želim upariti.

Tukaj je osnovna poizvedba:

INSERT INTO `yuoht_ucm_content` (
 `core_body`,
 `core_created_user_id`,
 `core_created_by_alias`,
 `core_created_time`,
 `core_modified_user_id`,
 `core_modified_time`,
 `core_publish_up`,
 `core_publish_down`,
 `core_images`,
 `core_urls`,
 `core_metakey`,
 `core_metadesc`,
 `core_metadata`
)
SELECT
`introtext` AS `core_body`,
`created_by` AS `core_created_user_id`,
`created_by_alias` AS `core_created_by_alias`,
`created` AS `core_created_time`,
`modified_by` AS `core_modified_user_id`,
`modified` AS `core_modified_time`,
`publish_UP` AS `core_publish_up`,
`publish_down` AS `core_publish_down`,
`images` AS `core_images`,
`urls` AS `core_urls`,
`metakey` AS `core_metakey`,
`metadesc` AS `core_metadesc`,
`metadata` AS `core_metadata`
FROM `yuoht_content`

Rad bi dodal še pogoj, nekaj v stilu:

WHERE `yuoht_content`.`id` = 172 AND `yuoht_ucm_content`.`core_content_id` = '2'

Torej hočem, da se skopirajo podatki iz tabele yuohtcontent iz vrstice z id-jem 172 v tabelo yuohtucm_content v vrstico z id-jem 2.

Pa mi nekako ne rata sestaviti delujoče poizvedbe s pogojem.

4 odgovori

Če imaš INSERT, pomeni, da vstavljaš nove recorde, ki še ne obstajajo in potem ne moreš dati pogoj na ta record.... če recordi že obstajajo, potem moraš uporabiti UPDATE namesto INSERT, če pa ne obstajajo, pa mi ni jasno, ka bi rad z :

yuoht_ucm_content.core_content_id = '2'

Ups, tole sem zabluzil.
Vrstic obstaja, seveda je potrebno narediti UPDATE.

Ampak še vedno mi ne rata na pravi način dodati pogoja

No, mi je ratalo:

UPDATE `yuoht_ucm_content` a, `yuoht_content` b
SET a.core_body = b.introtext,
a.core_created_user_id = b.created_by,
a.core_created_by_alias = b.created_by_alias,
a.core_created_time = b.created,
a.core_modified_user_id = b.modified_by,
a.core_modified_time = b.modified,
a.core_publish_up = b.publish_up,
a.core_publish_down = b.publish_down,
a.core_images = b.images,
a.core_urls = b.urls,
a.core_metakey = b.metakey,
a.core_metadesc = b.metadesc,
a.core_metadata = b.metadata
WHERE a.core_content_id = 2 AND b.id = 172

Malo se mi mudi pa sem skušal užgati bližnjico, namesto da bi dvakrat premislil :/

4