我已经搜索过了,但我不能让它自己工作
所以要完全清楚我有WP数据库,我有这样的meta_keys:
ID ------ POST_ID -------- META_KEY ---- META_VALUE
1 ------ 235 ------- book_price ---- 35$
2 ------ 256 ------- book_price ---- 65$
3 ------ 235 ------- tcp_book_price ---- 0
4 ------ 256 ------- tcp_book_price ---- 0
我希望tcp_book_price的meta_value从book_price接收具有相同post_id的meta_value
最终结果希望:
ID ------ POST_ID -------- META_KEY ---- META_VALUE
1 ------ 235 ------- book_price ---- 35$
2 ------ 256 ------- book_price ---- 65$
3 ------ 235 ------- tcp_book_price ---- 35$
4 ------ 256 ------- tcp_book_price ---- 65$
我尝试过UPDATE FROM SELECT但我无法让它工作..
提前致谢
好的,我自己用PHP脚本找到了解决方案:
<?php
$conn = mysql_connect("localhost", "root", "root");
if (!$conn) {
echo "Impossible de se connecter à la base de données : " . mysql_error();
exit;
}
if (!mysql_select_db("database_name")) {
echo "Impossible de sélectionner la base database_name : " . mysql_error();
exit;
}
$sql = "SELECT `post_id`, `meta_value` FROM `wp_postmeta` WHERE `meta_key` = 'book_price'";
// query the database
$resource = mysql_query($sql);
// loop through the results
while ($result = mysql_fetch_assoc($resource)) {
// grab the value
$price = $result['meta_value']; // you should use quotes here, btw
$pid = $result['post_id']; // you should use quotes here, btw
echo $price;
echo $pid;
// make your modifications;
// ++$nox; // or whatever
// build the query
$sql = "UPDATE `wp_postmeta` SET `meta_value` = '$price' WHERE `meta_key` = 'tcp_price' AND `post_id` = '$pid'";
// run the query
mysql_query($sql);
}
?>