【发布时间】:2016-10-27 16:51:50
【问题描述】:
1.我有两个数据库(即database_1m和database_current)
2.我在 include_status 表中插入了一些列(即在 database_1m 中)。我想在 database_current 中插入相同的表。
3.每个数据库在 database_1m 中都有时间字段,我将获得所有时间,但在第二个数据库中,我只需要最近的时间行,所以我使用 mysql 查询获取最近更新的时间行(即 select * from include_status where time =(select max(time) from include_status);)
4.这里我不知道如何使用 perl 在同一个子程序中使用两个数据库? 代码:
sub data
{
$DBH = &connect or die "Cannot connect to the sql server \n";
$DBH->do("USE database_1m;");
$stmt = "INSERT INTO include_status(time,available,closed,used,busy,reserved,down) VALUES(\"$current_time\",\"$include{'D'}\",\"$include{'B'}\",\"$include{'A'}\",\"$include{'C'}\",\"$include{'R'}\",\"$include{'+'}\")";
my $sth = $DBH->prepare( $stmt );
$sth->execute() or print "Could not insert data";
$sth->finish;
$DBH->disconnect();
}
Mysql查询:(插入最近更新的行)
select * from include_status where time=(select max(time) from include_status);
【问题讨论】:
-
我有点困惑。您想将表插入到不同的数据库中,还是将行插入到同一数据库的不同表中?我问是因为您提到“我想将相同的表从 database_1m 插入到 database_current”但代码显示
INSERT INTO include_status告诉我 include_status 是表名? -
我想将同一张表插入另一个数据库(即databse_Current)。但是第二个数据库应该只包含最近更新的时间行。@user2082599
-
如果该表不存在,则需要创建该表,然后通过从另一个表中选择插入到表中。 DBH 连接完全相同,但您只需添加 dbh2 作为连接名称,因为 dbh 已定义。
-
但是如何在 $stmt 变量@user2082599 中插入 mysql 查询
-
我现在将发布答案。