Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql中用数据生成表_Mysql_Database_Select_Insert Query - Fatal编程技术网

在mysql中用数据生成表

在mysql中用数据生成表,mysql,database,select,insert-query,Mysql,Database,Select,Insert Query,我有两个数据库 db1 db2 db1在控制器表的描述下面有表控制器 +-----------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+--------------+------+-----+---------+-------+

我有两个数据库

db1

db2

db1在控制器表的描述下面有表控制器

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| id                    | int(11)      | NO   | PRI | NULL    |       |
| interface_name        | varchar(255) | YES  |     | NULL    |       |
| store_number          | int(11)      | YES  |     | NULL    |       |
| file_seq_no           | int(11)      | YES  |     | NULL    |       |
| packet_seq_no         | int(11)      | YES  |     | NULL    |       |
| last_trans_start_date | date         | YES  |     | NULL    |       |
| last_trans_end_date   | date         | YES  |     | NULL    |       |
| last_extract_datetime | datetime     | YES  |     | NULL    |       |
| is_enabled            | char(1)      | YES  |     | NULL    |       |
+-----------------------+--------------+------+-----+---------+-------+
相同的表描述和附加的columndate_在数据库db2中以不同的名称打开,该数据库名为store table

我的工作是基于以下逻辑编写一个查询

第1点:从db2.store获取所有商店的编号、打开日期

第2点:将db1.controller表中的所有存储单元号提取到列表中

第3点:过滤从db2.store中提取的新store_编号,该编号在db1.controller中不存在

对于每一个新的门店编号

插入新存储的默认初始值,文件序号和数据包序号将设置为零

last_transaction_start_date、last_transaction_end_date、last_extract_timestamp将是存储创建日期,即db2.store.date_opened

启用将设置为1

主要问题是

Insert into db1.controller table values( store_number, 0, 0,$date_opened,$date_opened,
$date_opened,1);
请任何人帮我完成这个sql查询


提前感谢

您可以通过两种方式获得过滤后的数据

借助子查询

这里将显示db1.controller中不存在的所有新数据。您也可以在join的帮助下显示相同的结果

 SELECT db2.store.* FROM  db2.store  LEFT JOIN db1.controller ON db2.store.ID = db1.controller.ID WHERE db1.controller.ID IS NULL
您将获得db1中不存在的数据。controller现在迭代数据并执行insert查询

更新

您可以尝试在下面的单个查询中添加筛选的记录

Insert into db1.controller (id,interface_name,store_number,file_seq_no,packet_seq_no,last_trans_start_date,last_trans_end_date,last_extract_datetime,is_enabled)  SELECT db2.store.id, db2.store.interface_name, db2.store.store_number, 0, 0, db2.store.date_opened, db2.store.date_opened, db2.store.date_opened, db2.store.is_enabled  FROM  db2.store  LEFT JOIN db1.controller ON db2.store.ID = db1.controller.ID WHERE db1.controller.ID IS NULL;

这就是我所做的

插入到db1.0控制器中 接口名称、存储号、文件序号、数据包序号、最后传输开始日期、最后传输结束日期、最后提取日期时间已启用 选择 “测试”,s.store\u编号,0,0,s.date\u打开,s.date\u打开,s.date\u打开,'1' 从db2.store s,其中s.store_number不在select store_number中 从db1.1控制器


@JW,你能给我一些建议来完成它吗?谢谢你的回复,但我需要一个查询,它会将过滤后的记录插入到db1.controller表中。嗯,等待一段时间似乎很困难,也许社区会帮助你。@subodh添加了一个查询尝试一下。谢谢你的努力。我正在寻找一些不使用加入查询的东西。我给你+1作为你的努力。
Insert into db1.controller (id,interface_name,store_number,file_seq_no,packet_seq_no,last_trans_start_date,last_trans_end_date,last_extract_datetime,is_enabled)  SELECT db2.store.id, db2.store.interface_name, db2.store.store_number, 0, 0, db2.store.date_opened, db2.store.date_opened, db2.store.date_opened, db2.store.is_enabled  FROM  db2.store  LEFT JOIN db1.controller ON db2.store.ID = db1.controller.ID WHERE db1.controller.ID IS NULL;