通过PHP有条件地计算MYSQL列行(多个数据库)和输出数据

时间:2016-11-14 12:02:08

标签: php mysql union

我正在尝试运行查询并从同一服务器上的多个数据库(使用UNION子句)获取结果,但它不适用于我。所有数据库中的表名和列名相同(相同的杯子,相同的咖啡,不同的“商店”:D)。这是我的sql语句:

 $result = mysql_query("SELECT MONTH(ScanDate) AS sdmonth, YEAR(ScanDate) AS sdyear,  
              COUNT(Investigation) AS Investigation, COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END) AS MaleCount, 
              COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END) AS FemaleCount, SUM(InvestigationAmount) AS investigationAmount, SUM(AmountDue) AS AmountDue   
                    FROM `database1`.`table_name`
                    UNION
                    SELECT MONTH(ScanDate) AS sdmonth, YEAR(ScanDate) AS sdyear,  
              COUNT(Investigation) AS Investigation, COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END) AS MaleCount, 
              COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END) AS FemaleCount, SUM(InvestigationAmount) AS investigationAmount, SUM(AmountDue) AS AmountDue   
                    FROM `database2`.`table_name` 
                    WHERE (ScanDate BETWEEN '{$ScanDate1}' AND '{$ScanDate2}')
UNION
SELECT MONTH(ScanDate) AS sdmonth, YEAR(ScanDate) AS sdyear,  
              COUNT(Investigation) AS Investigation, COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END) AS MaleCount, 
              COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END) AS FemaleCount, SUM(InvestigationAmount) AS investigationAmount, SUM(AmountDue) AS AmountDue   
                    FROM `database3`.`table_name` 
                    WHERE (ScanDate BETWEEN '{$ScanDate1}' AND '{$ScanDate2}')");


    //-create  while loop and loop through result set
    while($row=mysql_fetch_array($result)){ 
    $month_doe=$row['sdmonth']; 
    $year_doe=$row['sdyear'];
    $si=$row['Investigation'];
    $male=$row['MaleCount'];
    $female=$row['FemaleCount'];
    $sum_investigation=number_format($si);
    $sia=$row['investigationAmount'];
    $sum_investigationamount=number_format($sia);
    $srd=$row['AmountDue'];
    $sum_rebatedue=number_format($srd);
    }

我绝对是在我的头上并做了很多错事,因为这只输出了第二个数据库的结果。有人能指出我正确的方向吗?感谢。

我的连接参数也出现问题,这是我的连接细节:

<?php  
    $conn = mysql_connect('localhost', 'root', '');
     if (!$conn)
    {
     die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("", $conn);
?>

2 个答案:

答案 0 :(得分:0)

有几件事值得一看。首先,默认情况下,MySQL中的UNION作为DISTINCT运行,丢弃它认为相等的行。

如果您想要返回所有行,则需要使用UNION ALL而不仅仅是UNION。

我会尝试使用UNION ALL。

如果仍然不起作用,那么您将以一种可能对它们的返回方式造成严重破坏的方式为所有变量提供别名。

我会修改它来删除第二个和第三个select语句的别名,如下所示:

"SELECT MONTH(ScanDate) AS sdmonth, YEAR(ScanDate) AS sdyear,  
          COUNT(Investigation) AS Investigation, COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END) AS MaleCount, 
          COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END) AS FemaleCount, SUM(InvestigationAmount) AS investigationAmount, SUM(AmountDue) AS AmountDue   
                FROM `database1`.`table_name`
                UNION ALL
                SELECT MONTH(ScanDate), YEAR(ScanDate),  
          COUNT(Investigation), COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END), 
          COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END), SUM(InvestigationAmount), SUM(AmountDue)   
                FROM `database2`.`table_name` 
                WHERE (ScanDate BETWEEN '{$ScanDate1}' AND '{$ScanDate2}')
UNION ALL
SELECT MONTH(ScanDate), YEAR(ScanDate),  
          COUNT(Investigation), COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END), 
          COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END), SUM(InvestigationAmount), SUM(AmountDue)
                FROM `database3`.`table_name` 
                WHERE (ScanDate BETWEEN '{$ScanDate1}' AND '{$ScanDate2}')"

希望有所帮助

答案 1 :(得分:0)

终于有效了。简单的答案很难看到。我需要一直做的SQL语句是这样的:

SELECT COUNT(Investigation) AS Investigation, COUNT(CASE WHEN `Gender` = 'Male' THEN 1 END) 
                        AS MaleCount, COUNT(CASE WHEN `Gender` = 'Female' THEN 1 END) AS FemaleCount, 
                        SUM(InvestigationAmount) AS investigationAmount, SUM(AmountDue) AS AmountDue   
                        FROM 
                        (SELECT * FROM `database1`.`table_name` WHERE ScanDate BETWEEN '{$ScanDate1}' 
                        AND '{$ScanDate2}'
                        UNION ALL
                        SELECT * FROM `database2`.`table_name` WHERE ScanDate BETWEEN '{$ScanDate1}' 
                        AND '{$ScanDate2}') table_alias_here

感谢您的输入人员。完全赞赏。