Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment fusionner une table dans MySQL

$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`";

Ajouter ; pour combiner deux instructions SQL.

Vous pouvez combiner ces requêtes comme ceci.

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

requête résultante :

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`

J'ai essayé cela en utilisant PhpMyAdmin. la requête a réussi

il ne peut pas être exécuté par mysql_query() . permet de trouver d'autres astuces. nous devons l'exécuter séparément.

Utilisez mysqli pour exécuter ceci

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