Mysql 为多个变量ID插入多个静态值

Mysql 为多个变量ID插入多个静态值,mysql,sql,wordpress,advanced-custom-fields,Mysql,Sql,Wordpress,Advanced Custom Fields,一些背景故事(与我的问题没有直接关系,但也许其他人可以使用我的方法) 我正在WordPressV3.9.1中使用插件高级自定义字段。我已经从旧数据库导入了自定义值的CSV文件(使用WP Ultimate CSV Importer插件,免费版本),该数据库的格式与我在WordPress中的需要相同,但有一个例外——ACF Repeater字段。这个插件很棒,但目前还没有一种导入大量数据的好方法 中继器字段存储在数据库中,如下所示: meta_id post_id meta_key

一些背景故事(与我的问题没有直接关系,但也许其他人可以使用我的方法)

我正在WordPressV3.9.1中使用插件高级自定义字段。我已经从旧数据库导入了自定义值的CSV文件(使用WP Ultimate CSV Importer插件,免费版本),该数据库的格式与我在WordPress中的需要相同,但有一个例外——ACF Repeater字段。这个插件很棒,但目前还没有一种导入大量数据的好方法

中继器字段存储在数据库中,如下所示:

meta_id  post_id meta_key           meta_value
3894       4697    beds               2
3895       4697    _beds              field_53bcfe244a98d
4051       4697    _beds_0_other      field_53c2273053218
4050       4697    beds_0_other       1
4051       4697    _beds_1_other      field_53c2273053218
4050       4697    beds_1_other       3

5894       4698    beds               2
5895       4698    _beds              field_53bcfe244a98d
5051       4698    _beds_0_other      field_53c2273053218
5050       4698    beds_0_other       1
5051       4698    _beds_1_other      field_53c2273053218
5050       4698    beds_1_other       3
就是,;对于每个post_id,有一个称为“床”的转发器字段。“In”中继器字段为1个字段,重复两次。每个字段有2个数据库条目—一个字段引用(用于管理保存字段—每个字段始终相同)和一个值。虽然设置不尽直观,但它是围绕WordPress的默认表格系统设计的

实际问题

现在,我从旧数据库导入了如下字段:

meta_id  post_id meta_key           meta_value
####       4697    beds               2
####       4697    beds_0_other       1
####       4697    beds_1_other       3

####       4698    beds               2
####       4698    beds_0_other       1
####       4698    beds_1_other       3
我需要补充一点

meta_id  post_id meta_key           meta_value
####       4697    _beds              field_53bcfe244a98d
####       4697    _beds_1_other      field_53c2273053218
####       4697    _beds_0_other      field_53c2273053218

####       4698    _beds              field_53bcfe244a98d
####       4698    _beds_1_other      field_53c2273053218
####       4698    _beds_0_other      field_53c2273053218
meta_键和meta_值是静态的-它们永远不会更改(创建字段后,在删除之前它将保留相同的字段)。meta_id是自动递增的

问题是我有200多个post_id值,每个值需要50个静态条目——不想硬编码。我可以使用以下选项选择所需的ID:

SELECT DISTINCT ID
FROM  `wp_posts`
WHERE post_type =  "community"

// Returns:
post_id
4697
4698
INSERT INTO `table` (`meta_id`, `post_id`, `meta_key`, `meta_value`)
VALUES
// foreach related distinct ID in wp_posts
 (NULL, 'ID', "_beds", "field_53bcfe244a98d"),
 (NULL, 'ID', "_beds_0_other", "field_53c2273053218"),
 (NULL, 'ID', "_beds_1_other", "field_53c2273053218")
// end foreach
简言之

我怎样才能做到以下几点:

SELECT DISTINCT ID
FROM  `wp_posts`
WHERE post_type =  "community"

// Returns:
post_id
4697
4698
INSERT INTO `table` (`meta_id`, `post_id`, `meta_key`, `meta_value`)
VALUES
// foreach related distinct ID in wp_posts
 (NULL, 'ID', "_beds", "field_53bcfe244a98d"),
 (NULL, 'ID', "_beds_0_other", "field_53c2273053218"),
 (NULL, 'ID', "_beds_1_other", "field_53c2273053218")
// end foreach
**临时溶液**

目前,我只是使用PHP转储了所有数据并将其上传到PHPMyAdmin中。可以编写一个插入的PHP循环,但我正在寻找一个无需上传新PHP文件(或sql)即可使用的MySQL解决方案

$ids=array(“4697”、“4698”);
回声'
插入到`表'(`meta\u id`、`post\u id`、`meta\u value`、`meta\u key`)
值
; foreach($id作为$id){ 回声' (NULL、“.$id.”、“1”、“床位”), (空,“.$id.”,“字段”\u 53bcfe244a98d“,”\u床”), (NULL、“.$id.”、“字段”\u 53c2273053218、“\u床位”\u 0\u其他”),
; }
最简单的方法是导入隐藏的自定义字段(前导下划线)以及元值。如果您只想清理数据,可以使用
联合选择
作为
插入
的输入-您不需要
元id
,它将自动分配。请注意,在执行ThingInsert之前,此SQL不会检查数据是否已经存在,因此请确保不会出现重复

您可以通过查询带有“beds”的
meta_key
的所有条目的
wp_postmeta
来获取所需的
post_id
。使用此
post\u id
可以对其他值进行硬编码,并将它们别名为
SELECT
语句中的列,然后用于
INSERT
的值

-- modify the wp_ prefix as needed
INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`)
-- get "_beds" key/value for all entries with meta key of "beds"
SELECT post_id, '_beds' AS meta_key, 'field_53bcfe244a98d' AS meta_value
FROM wp_postmeta WHERE meta_key = 'beds'
UNION 
-- get "_beds_0_other" key/value for all entries with meta key of "beds"
SELECT post_id, '_beds_0_other' AS meta_key, 'field_53c2273053218' AS meta_value
FROM wp_postmeta WHERE meta_key = 'beds'
UNION 
-- get "_beds_1_other" key/value for all entries with meta key of "beds"
SELECT post_id, '_beds_1_other' AS meta_key, 'field_53c2273053218' AS meta_value
FROM wp_postmeta WHERE meta_key = 'beds'
-- order results (you can view what will be inserted if you run the SELECT without the INSERT)
ORDER BY post_id
您还可以将其作为三个不同的
INSERT
语句运行,而不使用
UNION