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

时间:2014-07-14 06:06:12

标签: mysql sql wordpress advanced-custom-fields

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

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

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,有一个称为“床”的Repeater字段。 “在”中继器字段是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_key和meta_value是静态的 - 它们永远不会改变(在创建字段之后它保留相同的字段_ ##直到删除)。 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

**临时解决方案**

暂时,我只是使用PHP&amp ;;转储所有数据。上传到PHPMyAdmin。可以写一个插入的PHP循环,但我正在寻找一个MySQL解决方案,我可以使用而无需上传新的php文件(或sql)。

$ids = array("4697", "4698" );

echo '
INSERT INTO `table` (`meta_id`, `post_id`, `meta_value`, `meta_key`)
VALUES<br />';
foreach ($ids as $id){
    echo '
(NULL, "'. $id .'", "1", "beds"),
(NULL, "'. $id .'", "field_53bcfe244a98d", "_beds"),
(NULL, "'. $id .'", "field_53c2273053218", "_beds_0_other"),
<br />';

}

1 个答案:

答案 0 :(得分:1)

最简单的方法是导入隐藏的自定义字段(前导下划线)以及元值。如果您只想清理数据,可以使用UNION SELECT作为INSERT的输入 - 您不需要meta_id,它会自动分配。请注意,在执行插入操作之前,此SQL不会检查数据是否已存在,因此请确保您不会使用dupes。

您可以通过post_id查询wp_postmeta&#34; bed&#34;的所有条目,获得所需的meta_key。使用此post_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