将数据插入到具有多个数据库的表的列中并覆盖行

时间:2017-06-08 11:34:18

标签: php mysql database

我仍然是相对较新的PHP,并且对于我正在处理的以下查询,我将非常感谢任何帮助或正确方向上的一点。

我有两个数据库,从中我从每个数据库获取公司的呼叫计数(在特定日期范围内),然后将查询合并到一个表中并显示信息,以及两个计数之间的差异,如下:

ID |名称|日期| FirstDB计数| SecondDB计数|差异|

现在,我有两个主要问题。第一个是第二个数据库表确实包含公司的名称或ID。区分id的唯一方法是在表的名称中。例如,'Dial_Table_1',公司ID是1,'Dial_Table_2',id是2,等等。在DB中有一个公司表,它将id与名称相关联,所以我试图运行表名,将它们链接到ID,然后将其插入主查询。到目前为止,这是我的代码:

以下是我的功能:

function getCompanyName($conn, $company_id) {
    $sql_companies ="SELECT name AS company, id AS company_id
                FROM company
                WHERE status = 1
                AND id = '$company_id'";

    return $sql_companies;
}

function getFirstDBTables($conn) {
    $tables = [];

    $q = "SELECT DISTINCT job_table 
        FROM test_type";

    $_tables = $conn->query($q);
    while ($table = $_tables->fetch_assoc()) {
        $tables []= $table["job_table"];
    }
    return $tables;
}
function getSecondDBTables($conn_seconddb) {

    $dial_tables = [];
    $r = "SHOW TABLES LIKE 'Dial_Table_%'";

    $d_tables = $conn_seconddb->query($r);
    while ($d_table = $d_tables->fetch_assoc()) {
    $dial_tables []= $d_table["Tables_in_dial_reports (Dial_Table_%)"];
    }
    return $dial_tables;
}

function constructFirstQuery($conn, $startDate, $endDate, $company_id) {
    foreach(getFirstDBTables($conn) as $table) {
        $sql []= "SELECT 
            number.company_id AS company_id,
            com.name AS company,
            DATE(call_start_time) AS `date`,
            COUNT(*) AS  `sub_total`,
            '$table' AS `table`
            FROM $table
            LEFT JOIN number ON number.id = $table.number_id
            LEFT JOIN company AS com on com.id=number.company_id 
            WHERE $table.show IS TRUE
            AND call_start_time BETWEEN '$startDate 00:00:00' AND '$endDate 23:59:59'
            AND $table.processing_complete = 1
            GROUP BY `date`, company_id";
        }
    $sql = implode("\n UNION \n", $sql);
    return $sql = "SELECT company, company_id, `date`, SUM(sub_total) AS FirstDB_Count, 0 AS SecondDB_Count FROM ($sql) AS tempTable1 GROUP BY `date`, company_id";
}

function constructSecondQuery($conn_seconddb, $startDate, $endDate, $company_id) {
    foreach(getSeconddbTables($conn_seconddb) as $table) {
        $company_id = str_replace('Dial_Table_', '', $table);
        $sql_dt []= "SELECT 
                $company_id AS company_id,
                DATE(start_time) AS `date`, 
                COUNT(*) AS sub_total 
                FROM $table 
                WHERE $table.published = 1 
                AND start_time BETWEEN '$startDate 00:00:00' AND '$endDate 23:59:59'
                GROUP BY `date`, company_id"; 
        }
    }
    $sql_dt = implode("\n UNION \n", $sql_dt);
    return $sql_cr = "SELECT ' ' AS company, company_id,`date`, 0 AS FirstDB_Count, SUM(sub_total) AS SecondDB_Count FROM ($sql_dt) AS tempTable2 GROUP BY `date`, company_id";
}

这是我的主要代码块(我尝试了很多不同的想法,并对它们进行了评论):

<?PHP

$sql = "";
$sql_cr = ""; 
$sql_companies = "";
$name = "";

$sql = constructFirstQuery($conn, $startDate, $endDate, $company_id);
$result_firstdb = $conn->query($sql) or die($conn->error);

$sql_dt = constructSecondQuery($conn_seconddb, $startDate, $endDate, $company_id);
$result_dt = $conn_reportingdb->query($sql_dt) or die($conn_seconddb->error);

/***
* This will save companies ID and names to an array
* And will return an array of the companies
*/
// while ($row = $result_companies->fetch_assoc()) {
//     echo $row['company']. ' ';
//  }
$total_results = array();

while ($row = $result_firstdb->fetch_assoc()) {
    $total_results[$row['company_id']] = $row;
  }
while ($row = $result_dt->fetch_assoc()) {

    //$company_id = $row['company_id'];

    // $sql_companies = getCompanyNames($conn, $company_id);
    // $result_companyNames = $conn->query($sql_companies) or die($conn->error);
    // while ($row2 = $result_companies->fetch_assoc()){
    //     $total_results[$row[$company_id]] = $row2['company_id'];
       //}

if(!isset($total_results[$row['company_id']])) {
    $total_results[$row['company_id']] = $row;

    $company_id = $row['company_id'];
    $sql_companies = getCompanies($conn, $company_id);
    $result_companyNames = $conn->query($sql_companies) or die($conn->error);
    while ($row2 = $result_companyNames->fetch_assoc()){
        $total_results[$row[$company_id]] = $row2['company'];
        //$company_id = $row2['company'];

       //if(!isset($total_results[$row['company']])){
           // $total_results[$row[$company_id]] = $row2['company'];
      //} else {
           //$total_results[$row['company_id']['company']] = $row2['company'];
        //}
    }
} else {
    $total_results[$row['company_id']]['SecondDB_Count'] = $row['SecondDB_Count'];    //**Merging the databases 
  }
}

//array_push($company_data, $total_results);
//print_r($company_data);

//print_r($company_data[$row['company_id']]);
// if(!isset($company_data[$row['company_id']])) {
//       $company_data[$row['company_id']] = $row;
//       //print_r($row);

// } else {
//       $company_data[$row['company_id']]['Spearline_total'] = $row['Spearline_total'];    //**Merge 
//     }


// while ($row = $result_cr->fetch_assoc()) {
//     if(!isset($company_data[$row['company_id']])) {
//       $company_data[$row['company_id']] = $row;
//     } else {
//       $company_data[$row['company_id']]['Reporting_total'] = $row['Reporting_total'];    //**Merge 
//     }
//   }

// }
// }

if ($total_results) {
    echo"<TABLE><TR><TH>Company</TH>
    <TH>Company ID</TH>
    <TH>Date</TH>
    <TH>Firstdb Total</TH>
    <TH>Seconddb Total</TH>
    <TH>Difference</TH></TR>";

    foreach($total_results as $row) {
        $firstdb_total = $row['FirstDB_total'];
        $seconddb_total = $row['SecondDB_total'];
        $difference = ($firstdb_total - $seconddb_total);

        echo"<TR><TD>". $row["company"]. "</TD>";
        echo"<TD>". $row["company_id"]. "</TD>";
        echo"<TD>". $row["date"]. "</TD>";
        echo"<TD>". $row["FirstDB_total"]. "</TD>";
        echo"<TD>". $row["SecondDB_total"]. "</TD>";
        echo"<TD>". $difference . "</TD></TR>";

      }
      echo"</TABLE>";
    }

所以第一个主要问题是每个数据库中的名称与ID的匹配。在数据库1中,数据库2中的某些公司没有调用计数,因此该表目前给出了DB1中公司的名称,但如果它们仅包含在DB2中,则不返回该名称到桌子。如果有人碰巧知道如何解决这个问题,我将非常感激。

第二个问题是,在返回的表中,日期会相互覆盖。因此,对于每个公司而言,不是为每个指定的日期获取调用计数,而是每次循环时都会覆盖这些行,并且无论指定了多少天,我只会为每个公司获得一行计数。

非常感谢任何一个问题的帮助。正如我所提到的,我仍在学习这一点,并希望得到一些帮助。感谢

0 个答案:

没有答案