使用PHP运行复杂的MySQL QUERY

时间:2014-08-23 15:01:41

标签: php mysql sql

我尝试使用下面给出的复杂查询从名为'gainfinal'的表中获取数据:

    SELECT  g.countrycode,
        sum(case when `year` = '1995' then g.values else 0 end) AS "1995",
        sum(case when `year` = '1996' then g.values else 0 end) AS "1996",
        sum(case when `year` = '1997' then g.values else 0 end) AS "1997",
        sum(case when `year` = '1998' then g.values else 0 end) AS "1998", 
        sum(case when `year` = '1999' then g.values else 0 end) AS "1999",
        sum(case when `year` = '2000' then g.values else 0 end) AS "2000",
        sum(case when `year` = '2001' then g.values else 0 end) AS "2001",
        sum(case when `year` = '2002' then g.values else 0 end) AS "2002",
         sum(case when `year` = '2003' then g.values else 0 end) AS "2003", 
         sum(case when `year` = '2004' then g.values else 0 end) AS "2004", 
         sum(case when `year` = '2005' then g.values else 0 end) AS "2005",
        sum(case when `year` = '2006' then g.values else 0 end) AS "2006",
        sum(case when `year` = '2007' then g.values else 0 end) AS "2007",
        sum(case when `year` = '2008' then g.values else 0 end) AS "2008",
        sum(case when `year` = '2009' then g.values else 0 end) AS "2009", 
         sum(case when `year` = '2010' then g.values else 0 end) AS "2010", 
         sum(case when `year` = '2011' then g.values else 0 end) AS "2011",
        sum(case when `year` = '2012' then g.values else 0 end) AS "2012"


FROM    `gainfinal` g
WHERE   `year` between '1995' and '2012'
GROUP BY `countrycode`

我确信Query运行良好,因为它在Xampp中运行时返回了正确的数据。

我的PHP代码是这样的:

    ini_set('display_errors', 1); 
    $username = "root"; 
    $password = "";   
    $host = "localhost";
    $database="climate";
     //$country = 'NPL';
    // $indices = 'foodfinal';
   // $country=$_GET["country"];

    // $indices=$_GET["indices"];


    $server = mysql_connect($host, $username, $password);
    $connection = mysql_select_db($database, $server);

    $myquery = "SELECT  g.countrycode,sum(case when `year` = '1995' then `g.values` else 0 end) AS "1995",
        sum(case when `year` = '1996' then g.values else 0 end) AS "1996",
        sum(case when `year` = '1997' then g.values else 0 end) AS "1997",
        sum(case when `year` = '1998' then g.values else 0 end) AS "1998", 
        sum(case when `year` = '1999' then g.values else 0 end) AS "1999",
        sum(case when `year` = '2000' then g.values else 0 end) AS "2000",
        sum(case when `year` = '2001' then g.values else 0 end) AS "2001",
        sum(case when `year` = '2002' then g.values else 0 end) AS "2002",
         sum(case when `year` = '2003' then g.values else 0 end) AS "2003", 

         sum(case when `year` = '2004' then g.values else 0 end) AS "2004", 
         sum(case when `year` = '2005' then g.values else 0 end) AS "2005",
        sum(case when `year` = '2006' then g.values else 0 end) AS "2006",
        sum(case when `year` = '2007' then g.values else 0 end) AS "2007",
        sum(case when `year` = '2008' then g.values else 0 end) AS "2008",
        sum(case when `year` = '2009' then g.values else 0 end) AS "2009", 
         sum(case when `year` = '2010' then g.values else 0 end) AS "2010", 
         sum(case when `year` = '2011' then g.values else 0 end) AS "2011",
        sum(case when `year` = '2012' then g.values else 0 end) AS "2012"


FROM    `gainfinal` g
WHERE   `year` between '1995' and '2012'
GROUP BY `countrycode`";
    $query = mysql_query($myquery);

    if ( ! $query ) {
        echo mysql_error();
        die;
    }

    $data = array();

    for ($x = 0; $x < mysql_num_rows($query); $x++) {
        $data[] = mysql_fetch_assoc($query);
    }

    echo json_encode($data);  
     mysql_close($server);
      //header('Location: linegraph.html');
     // include( "linegraph.html");

?>

此PHP文件已返回其他查询的正确数据。它只适用于此查询onl。它说,在运行PHP文件时 :解析错误:语法错误,第17行的C:\ xampp \ htdocs \ climateapp \ data \ chloroplath \ data.php中的意外T_LNUMBER。如何使用PHP运行查询。

3 个答案:

答案 0 :(得分:4)

在我看来,您可以按国家/地区代码分组,您应该能够获得相同的结果:

SELECT  
  g.countrycode,
  g.year,
  SUM(g.values) as summed
FROM    `gainfinal` g
WHERE   g.year between '1995' and '2012'
GROUP BY g.countrycode, g.year

此外,您是否计划每年在查询中添加?对我来说似乎做了很多工作。

答案 1 :(得分:1)

我认为你的报价是关闭的。围绕的双引号:

     sum(case when `year` = '2003' then g.values else 0 end) AS "2003", 

正在影响php代码。最简单的解决方案是用反引号替换它们:

     sum(case when `year` = '2003' then g.values else 0 end) AS `2003`,

或者,使用不需要引用的名称:

     sum(case when `year` = '2003' then g.values else 0 end) AS value_2003,

编辑:

第一行中也有问题。这一行:

SELECT  g.countrycode, sum(case when `year` = '1995' then `g.values` else 0 end) AS "1995",

应该是:

SELECT g.countrycode, sum(case when `year` = '1995' then g.`values` else 0 end) AS `1995`,

答案 2 :(得分:1)

您在查询中使用双引号,这会过早地终止字符串。请考虑使用nowdoc代替:

<?php
$myquery = <<<'END_OF_QUERY'
SELECT  g.countrycode,sum(case when `year` = '1995' then `g.values` else 0 end) AS "1995",
        sum(case when `year` = '1996' then g.values else 0 end) AS "1996",
        // rest of query
END_OF_QUERY;

当您不希望解析内容时,Nowdocs是一种定义多行“原始”字符串(如查询)的便捷方式。这允许您在查询中使用所需的任何引号。