[英]Copy only unique entries from one table to another MySQL
I'm trying to create scheduled auto-updater for MySQL DB. 我正在尝试为MySQL DB创建计划的自动更新程序。 The script I use is shown below. 我使用的脚本如下所示。 It's idea is to insert only unique products from table wp_posts to table export_copy. 它的想法是仅将表wp_posts中的唯一产品插入表export_copy中。 But this one inserts nothing. 但是这个没有插入任何东西。 Please help me to fix the script. 请帮助我修复脚本。
INSERT INTO `export_copy`(`name`, `link`) SELECT p.post_title, p.guid FROM wp_posts p WHERE NOT EXISTS ( SELECT * FROM export_copy, wp_posts WHERE export_copy.id=wp_posts.ID)
Your query will never work! 您的查询将永远无法进行!
Since your table's name is "wp_posts" ,it seem you want copy data from wordpress table to your own 由于表的名称为“ wp_posts”,看来您想将数据从wordpress表复制到自己的表中
you can handle this by php, firt you must fetch data from table and save that to an array, then export array to insert query 您可以通过php处理此问题,首先必须从表中获取数据并将其保存到数组中,然后导出数组以插入查询
$link = mysqli_connect(Your_HOST , Your_User , YOUR_Pass, DB_NAME);
$q = "SELECT p.post_title, p.guid FROM wp_posts p WHERE NOT EXISTS ( SELECT * FROM export_copy, wp_posts WHERE export_copy.id=wp_posts.ID)";
$result = mysqli_query($link, $q);
$i=0;
while($row=mysqli_fetch_assoc($result))
{
$a[$i]['name'] = $row['post_title'];
$a[$i]['link'] = $row['post_title'];
$i++;
}
$q = '';
foreach($a as $a)
$q .= "('".$a['name']."','".$a['link']."'),";
$q = substr($q,0, (strlen($q)-1));
$q = "INSERT INTO `export_copy`(`name`, `link`) VALUES " . $q;
$result = mysqli_query($link, $q);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.