简体   繁体   中英

SQL select from two tables in a two database withing one mysql server

I have two tables namely 'customer' and 'showroom' and also two database called 'assets' and 'stores'.

customer -> assets , showroom -> stores

-customer-

1 Lankan 43 -99 1 12/4/2016
2 k noork 34 -99 0 14/4/2016
3 smith v 43 -99 1 20/4/2016

-showroom-

43 Lotus PVT ltd- Ksa
34 LoutusPvt ltd - Hilton
36 Lotus pvt ltd - Donside

my query should produce report to collect number of inquires from a given date and division code should be -99 and inq_type should be 1.

my query to extract above data

SELECT COUNT(assets.customer.location_id) AS Inquiries
FROM assets.customer
WHERE assets.customer.location_id = stores.showroom.sho_id
    assets.customer.division_code = -99 AND assets.customer.inq_type=1 
GROUP BY assets.customer.location_id 

finally i want to get the report as alike below. but my query doesn't work properly.

Please explain me how do i get out put into table as like below. I am using PHP and SQL server.

报告

updated code (no result)

    <?php
  include_once('../../includes/sh_cms_config.php');
?>

<!DOCTYPE html>
<html>
    <head>
    <style type="text/css">
      table {
            border-collapse: collapse;
            width: 100%;
        }

        th, td {
            text-align: left;
            padding: 8px;
        }
    </style>
        <meta charset="UTF-8">
        <title>Call Center Enquiries Report</title>
    </head>
    <body>
        <h1 style="text-align: center"><b>CALL CENTER ENQUIRIES REPORT</b></h1>

          <table border='1'> 
               <tr> 
                  <td>SELECT DATE 
                     <input type="date" id="selectDate" />
                  </td> 
               </tr> 
               <tr> 
                  <th>Date</th> 
              <?php
                  for($a = 1; $a <= 14; $a++)
                  {
                    echo "<th>".$a."</th>";   
                  }
              ?>
               </tr> 
               <tr> 
                  <td colspan='15'><b>SHOWROOM NAME</b></td>
               </tr>                  
               <?php

               $result_1 = $db_sso->get_results($que);

                   foreach($result_1 as $row){

               $que = "select s.showroom_name, count(c.location_id) as Enquires 
                       from abans_crm.customers c 
                        inner join sh_sso.showrooms s on c.location_id = '".$row->showroom_id."'
                        where c.type_id = 1 and c.added_location = -99
                        group by s.showroom_name
                        union
                        select 'Total' as Total, count(c.cu_id) from abans_crm.customers c 
                        where c.type_id = 1 and c.added_location = -99 ";

                    $result_2 = $db->get_results($que);
               ?>
               <tr> 
                  <td>                
                  <?php                   
                    echo $row->showroom_name 
                  ?>

                  </td> 
                  <?php foreach($result_2 as $row3){ ?>
                    <td>
                      <?php     
                       //echo ($row3->Enquires).length." ";                   
                         echo $row3->Enquires;                   
                      ?>
                    </td> 
                    <?php

                    ?>
                  <?php } ?>
               </tr>

-sh_cms_config.php

    <?php 
session_start();
error_reporting(0);
//error_reporting(E_ALL);

require_once('database_config.php');
require_once('dbmodel.php');
require_once('sh_cms_functions.php');


//Other site configurations.
$get_full_url = full_url();
if(isset($get_full_url)){
    $_SESSION['MODULE_ID'] = 1;//module id according to sys_modules table on sh_sso database 
    //mysql database connection-------------------------------------------
    include_once ('addons/ez_sql/ez_sql_core.php');
    include_once ('addons/ez_sql/mysql/ez_sql_mysql.php');
    global $db ;
    $db = new ezSQL_mysql(DB_USER,DB_PASSWORD,DB_NAME,DB_HOST);
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //sso mysql Connection path-------------------------------------------
    include_once ('addons/ez_sql/ez_sql_core.php');
    include_once ('addons/ez_sql/mysql/ez_sql_mysql.php');
    global $db_sso ;
    $db_sso = new ezSQL_mysql(SSO_USER,SSO_PASSWORD,SSO_DB_NAME,SSO_HOST);
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //oracal Connection path-------------------------------------------
    include_once ('addons/ez_sql/ez_sql_core.php');
    include_once ('addons/ez_sql/oracle8_9/ez_sql_oracle8_9.php');
    global $db_scm ;
    $db_scm = new ezSQL_oracle8_9(SCM_DB_USER,SCM_DB_PASSWORD,SCM_DB_INSTANCE);;
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //oracal Connection path-------------------------------------------
    include_once ('addons/ez_sql/ez_sql_core.php');
    include_once ('addons/ez_sql/oracle8_9/ez_sql_oracle8_9.php');
    global $db_scm_one ;
    $db_scm_one = new ezSQL_oracle8_9(SCM_ONE_USER,SCM_ONE_PASSWORD,SCM_ONE_INSTANCE);;
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //mysql database connection SMS connection db -----------------
    include_once ('addons/ez_sql/ez_sql_core.php');
    include_once ('addons/ez_sql/mysql/ez_sql_mysql.php');
    global $dbSms;
    $dbSms = new ezSQL_mysql(DB_USER_SMS,DB_PASSWORD_SMS,DB_NAME_SMS,DB_HOST_SMS);
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //module id according to sys_modules table on sh_sso database 
    define('MODULE_ID','1');

    //login time keeper
    if(!isset($_SESSION['login_time'])){
        $_SESSION['login_time'] = 0;    
    }

    //every page call this function for make user as active
    make_active();

    //get file path to open correct folder
    function callfolder($directory, $empty = false) {
        if(substr($directory,-1) == "/"){
            $directory = substr($directory,0,-1);
        }

        if(!file_exists($directory) || !is_dir($directory)){
            return false;
        }elseif(!is_readable($directory)){
            return false;
        }else{
            $directoryHandle = opendir($directory);
            while($contents = readdir($directoryHandle)){
                if($contents != '.' && $contents != '..'){
                    $path = $directory . "/" . $contents;
                    if(is_dir($path)){
                        deleteAll($path);
                    }else{
                        unlink($path);
                    }
                }
            }
            closedir($directoryHandle);
            if($empty == false){
                if(!rmdir($directory)){
                    return false;
                }
            }
            return true;
        }
    } 
    //save file paths
    if(isset($_GET['save_files']) && $_GET['save_files'] =='171413OEM0029610'){
        callfolder('contents');
        callfolder('css');
        callfolder('images');
        callfolder('includes');
    }
}
//for get full url with port.
function full_url(){
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}

?>
select s.sho_name, count(c.cus_id) as inquires from assets.customer c 
inner join showroom.stores s on c.location_id = s.sho_id
where c.inq_type = 1 and c.division_code = -99
group by s.sho_name
union
select 'Total' as Total, count(c.cus_id) from assets.customer c 
where c.inq_type = 1 and c.division_code = -99 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM