我有以下最终将成为网页的 perl 代码:
my($dbh) = DBI->connect("DBI:mysql:host=dbsrv;database=database","my_sqlu","my_sqlp") or die "Canny Connect";
my($sql) = "SELECT * FROM hardware where srv_name = \"$srv_name\"";
my($sth) = $dbh->prepare($sql);
$sth->execute();
$sth->bind_col( 1, \my($db_id));
$sth->bind_col( 2, \my($db_srv_name));
$sth->bind_col( 5, \my($db_site));
$sth->fetchrow();
$sth->finish ();
my($sql) = "SELECT sites.\`site_code\`, sites.\`long_name\` FROM \`hardware\` JOIN \`sites\` ON \`sites\`.id=\`hardware\`.\`site\` where \`hardware\`.\`id\`=\'$db_id\'";
my($sth) = $dbh->prepare($sql);
$sth->execute();
$sth->bind_col( 1, \my($db_site_code));
$sth->bind_col( 2, \my($db_long_name));
$sth->fetchrow();
$sth->finish ();
$dbh->disconnect;
print "$db_site_code<br>$db_long_name";
上面的查询确实有效,但是我想知道的是有什么方法可以运行一个 SQL 查询并从站点数据库中获取 db_site_code 和 db_long_name 而无需运行第二个查询?硬件数据库在站点数据库中有外键“id”。
当您阅读有关关系数据库的任何内容时,他们都说这是迄今为止从数据库中获取数据的最有效方法,但我看不出这比仅运行 2 个选择查询要快得多。我上面所做的肯定会比"select from hardware where srv_name = $srv_name"
那时更长"select from sites where id = db_site_id"
吗?任何意见都非常感谢。