sql >> Database teknologi >  >> RDS >> Mysql

Sådan flettes tabel i 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`";

Tilføj ; at kombinere to SQL-sætninger.

Du kan kombinere disse forespørgsler på denne måde.

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

resulterende forespørgsel :

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`

Jeg har prøvet dette ved hjælp af PhpMyAdmin. forespørgslen lykkedes

det kan ikke udføres af mysql_query() . lad os finde nogle andre tricks. vi er nødt til at udføre det separat.

Brug mysqli til at udføre dette

$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. MySQL - Dynamic Pivot Table Grouping Issue

  2. Databaseløsninger til byggeledelse

  3. Ikke-primære fremmednøgler i Django

  4. Erstat første forekomst af understreng i en streng i SQL