MySql INSERT WHERE然后SELECT子查询

时间:2012-09-11 15:52:18

标签: mysql

我试图将值插入到表中,只能通过在另一个表上执行SELECT WHERE来找到该行的ID。以下是我的询问:

INSERT INTO wp_postmeta (meta_key, meta_value) 
VALUES ('editor_review','5') 
WHERE post_id=(SELECT ID from wp_posts 
               WHERE post_title 
              LIKE "%The Honest Kitchen ZEAL Grain Free Gluten Free All Life Stages Dog Food%");

有任何想法吗?

由于

2 个答案:

答案 0 :(得分:0)

INSERT INTO wp_postmeta
    (post_id, meta_key, meta_value)
    SELECT id, 'editor_review', '5'
        FROM wp_posts
        WHERE post_title LIKE '%The Honest Kitchen ZEAL Grain Free Gluten Free All Life Stages Dog Food%'

答案 1 :(得分:0)

您可以使用INSERT INTO...SELECT语句。

INSERT INTO wp_postmeta (meta_key, meta_value) 
SELECT colA, colB
FROM wp_posts 
WHERE post_title 
LIKE "%The Honest Kitchen ZEAL Grain Free Gluten Free All Life Stages Dog Food%"

注意列数必须相互匹配(也是它们的数据类型)