使用select语句中的id插入wp_postmeta表

时间:2014-12-01 20:39:30

标签: mysql wordpress phpmyadmin wordpress-plugin

我有以下查询:

    SELECT  ID 
    FROM `wp_posts` as ps 
    INNER JOIN `wp_term_relationships` as tr 
    ON ps.ID = tr.object_id 
    WHERE ps.post_date < '2011-06-07' /*Get posts before June 6th 2011*/ 
    AND ps.post_title 
    LIKE '%MY SEARCH STRING %' /*A String im looking for in post titles*/ 
    AND tr.term_taxonomy_id = '548'/*My Category ID*/;

我需要查询为每个结果ID插入特定数据。理论上:

 INSERT IGNORE INTO wp_postmeta (post_id,meta_key, meta_value)
 VALUES (IDs from selection above,_thumbnail_id,11059)

这会在上面特定选项的所有帖子中将meta_key设置为_thumbnail_idmeta_value11059

使用wp_postmeta而不是meta_id

跳过将任何值插入wp_postmeta (post_id ..)自动增量列的wp_postmeta (meta_id,post_id ..)自动增量列

2 个答案:

答案 0 :(得分:0)

INSERT IGNORE INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID, '_thumbnail_id', 11059
FROM `wp_posts` as ps 
INNER JOIN `wp_term_relationships` as tr ON ps.ID = tr.object_id 
WHERE ps.post_date < '2011-06-07'
AND ps.post_title LIKE '%MY SEARCH STRING %'
AND tr.term_taxonomy_id = '548'

答案 1 :(得分:0)

INSERT IGNORE INTO wp_postmeta
SELECT ID, NULL, 11059
FROM `wp_posts` as ps 
INNER JOIN `wp_term_relationships` as tr ON ps.ID = tr.object_id 
WHERE ps.post_date < '2011-06-07'
AND ps.post_title LIKE '%MY SEARCH STRING %'
AND tr.term_taxonomy_id = '548';