如何在MySQL中合并表

时间:2012-09-07 08:21:13

标签: php mysql

我只想将这些表合并/组合成一个查询,但它不起作用。实际上有太多的表和列,但是对于这个例子我只是放了一些。

$insertSQL_sr1 = "INSERT INTO sr1_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr1`";

$insertSQL_sr2 = "INSERT INTO sr2_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr2`";

$insertSQL_sr3 = "INSERT INTO sr3_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr3`";

$insertSQL_full = $insertSQL_sr1.";".$insertSQL_sr2.";".$insertSQL_sr3;

mysql_query($insertSQL_full);

mysql_select_db($database_pods, $pods);
  $Result1 = mysql_query($insertSQL_full, $pods) or die(mysql_error());

出现错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO sr2_full (`date`, `total_pending`, `appt_today`, `percent_appt_today' at line 1

我的第二次编辑:

$insertSQL = "INSERT INTO sr1_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr1`; INSERT INTO sr2_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr2`; INSERT INTO sr3_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr3`";

mysql_select_db($database_pods, $pods);
  $Result1 = mysql_query($insertSQL, $pods) or die(mysql_error());

出现错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO sr2_full (`date`, `total_pending`, `appt_today`, `percent_appt_today' at line 1

2 个答案:

答案 0 :(得分:1)

$insertSQL1 = "INSERT INTO sr1_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr1`";

$insertSQL2 = "INSERT INTO sr2_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr2`";

$insertSQL3 = "INSERT INTO sr3_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr3`";

添加;以组合两个SQL语句。

您可以像这样组合这些查询。

$insertSQL_Single = $insertSQL1.";".$insertSQL2.";".$insertSQL3;

结果查询:

INSERT INTO sr1_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr1`;INSERT INTO sr2_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr2`;INSERT INTO sr3_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today` FROM `sr3`

我使用PhpMyAdmin尝试过这个。查询是成功的

mysql_query()无法执行。让我们找一些其他的技巧。我们需要分开执行它。

使用mysqli执行此

$mysqli = new mysqli("host","username","password", "databasename");
if (!$mysqli->multi_query($insertSQL_Single)) {
    echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
else
{
    echo "success";
}

答案 1 :(得分:0)

啊,我认为他想将数据从一个表复制到另一个表。只需一步读出它,将它临时存储在一些PHP变量中,然后将其插入另一步。