从表的列插入数据并存储到另一个表的列 [英] Insert data from column of a table and store to column another table

查看:29
本文介绍了从表的列插入数据并存储到另一个表的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个表 Sourcedetails 与此列:

I have 2 table Source and details with this column:

Source id, item_name, items_download id 是主键.

details name, downloadand ...name` 为主键.

details name, downloadand ...name` is primary key.

我想从源表(实时)获取数据并放入details 表中.这是我的代码:

I want to get data from Source table (in real time) and put into details table. This is my code:

$get= "INSERT INTO `details` (`name`,  `download`) SELECT `Source`.`item_name`,`Source`.`items_download` FROM `Source`"

上述代码有效,但仅在第一次插入数据时,刷新页面时出现此错误:

The above code worked, but only for the first time and data inserted, when I refresh the page I got this error:

Duplicate entry 'Test1' for key 'PRIMARY'

推荐答案

如要求 UPDATE 检查,如果您想知道数据库表是否已导入或更新,请先运行此 SCRIPT:

As asked for the UPDATE to check, if you want to know whether the database-table was imported OR updated, run this SCRIPT first:

SELECT 
    COUNT(*) AS countOfDetails 
FROM 
    details

在 PHP 中:

$messageString = "";
// assign the countOfDetails from the Query to a PHP variable $myCount
if($myCount==0){
    $messageString = "Database Imported Successfully!";
}
else{
    $messageString = "Database Updated Successfully!";
}

获取要显示在字符串中的消息后.
您可以运行此脚本来更新/导入数据库表:

After getting the message to be displayed in a string.
You can run this SCRIPT to update/import the Database Table:

INSERT INTO details (name, download) 
SELECT item_name, items_download
FROM Source
WHERE NOT EXISTS 
          ( SELECT name, download FROM details 
            WHERE name IN (
                       SELECT item_name FROM Source
                          )
          ) 

运行后,在任何你想要的地方回显$messageString.

After you have Run it, echo the $messageString wherever you want.

注意: 这将插入详细信息表中不存在的行(并且不会给出错误,如果已经存在,则会跳过 INSERT 部分),因此重复的错误在任何情况下都不会给出,工作将完成

NOTE: This will insert the rows that are not present in the details table (and won't give an error and will skip the INSERT part if it already exists), so the error of Duplication will be not given in any case and the work will be done

这篇关于从表的列插入数据并存储到另一个表的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆