简体   繁体   English

使用变量mysql从select语句插入多行

[英]Insert multiple rows from select statement with variable mysql

Here's the sql statement I tried for adding data from one table to another. 这是我尝试将数据从一个表添加到另一个表的sql语句。

INSERT INTO wp_postmeta(`post_id`,`meta_key`,`meta_value`) VALUES((SELECT ID FROM wp_posts tb WHERE tb.post_type = 'support'),'category_order','0')

Basicaly I want to add rows to the wp_postmeta table. 基本上我想向wp_postmeta表添加行。 I want to add two static values, meta_key and meta_value. 我想添加两个静态值meta_key和meta_value。 But I also want to add post_id which should be derived from the ID of each row in wp_posts. 但我也想添加post_id,它应该从wp_posts中每一行的ID派生。 This doesn't seem to work when there are multiple rows returned from the SELECT statement. 当SELECT语句返回多个行时,这似乎不起作用。

Thanks for the help. 谢谢您的帮助。

试试这个

INSERT INTO wp_postmeta(post_id,meta_key,meta_value) SELECT ID, 'category_order','0' FROM wp_posts tb WHERE tb.post_type = 'support'

INSERT INTO wp_postmeta(`post_id`,`meta_key`,`meta_value`) SELECT ID, 'category_order','0' FROM wp_posts tb WHERE tb.post_type = 'support'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM