I'm sure this has been asked before but I can't quite make the connection from what I'm trying to do with what I have found searching. I think this is because I want to do it from a second table and with a non key field.
Basically I have this curriculum.keywords table which contains keywords for different courses.
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| course_id | int(11) | NO | MUL | NULL | |
| keyword | varchar(100) | NO | MUL | NULL | |
| excerpt | longtext | NO | | NULL | |
| explained | longtext | NO | | NULL | |
| slug | varchar(100) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
and I have an extract from an old wordpress website which contains lots of theory notes with titles that match some of the keywords in the table above.
+------------------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+----------------+------+-----+---------+-------+
| wp_ID | int(4) | YES | | NULL | |
| wp_post_date | varchar(19) | YES | | NULL | |
| wp_post_modified | varchar(19) | YES | | NULL | |
| wp_post_status | varchar(7) | YES | | NULL | |
| wp_post_title | varchar(35) | YES | | NULL | |
| wp_post_content | varchar(10435) | YES | | NULL | |
| wp_post_excerpt | varchar(224) | YES | | NULL | |
| wp_post_parent | int(1) | YES | | NULL | |
| wp_post_name | varchar(33) | YES | | NULL | |
| wp_post_type | varchar(8) | YES | | NULL | |
+------------------+----------------+------+-----+---------+-------+
If the keyword is found I want to update the excerpt and explained fields with the old wordpress data giving it a course id of 2. If the keyword does not exist I would like to insert a new row and give it a course_id of 4.
I started with this from here but cannot quite finish it off.
IF NOT EXISTS (SELECT * FROM `curriculum.keywords` WHERE `keyword` = STUCK HERE) THEN
INSERT INTO `curriculum.keywords` (`keyword`, `excerpt`, `explained`, `slug`, `course_id`)
SELECT `wp_post_title`, `wp_post_excerpt`, `wp_post_content`, `wp_post_name`, 4 FROM `aimport-notes`
ELSE
UPDATE `curriculum.keywords` (`keyword`, `excerpt`, `explained`, `slug`, `course_id`)
SELECT `wp_post_title`, `wp_post_excerpt`, `wp_post_content`, `wp_post_name`, 4 FROM `aimport-notes`
WHERE `curriculum.keywords`.`keyword` = `aimport-notes`.`wp_post_title`
END IF;
Thanks in anticipation of your help.
Chris