如何使用会话使用SQL查询过滤数据

时间:2016-06-15 12:35:22

标签: php mysql sql session

我有一个数据库,用于创建带有D3.js的饼图。我已经使用表单按年份和月份对数据进行排序,当您提交时,会根据下拉选项创建会话。我在查询中使用会话。

enter image description here

我附上了一张图片以便更好地理解,目前我的查询是将整体数据排序在一起但不是独立排序。例如,我想在1月份对2016年的数据进行排序。

我一直在玩它,但我似乎无法做到正确,他们应该两者彼此独立一起工作,我有得到的只是另一个。这是我的代码

// this is used to reset the graphs back to default 
            if ($_SESSION['filteryear'] == 0 && $_SESSION['filtermonth'] == 0) {
                $sql = "SELECT value, COUNT(*) AS count
                FROM sodsurvey LEFT OUTER JOIN age
                ON sodsurvey.age_id = age.id 
                WHERE value IS NOT NULL AND office_id = " . $office_id . " 
                GROUP BY age_id; ";
            } else {
                $sql = "SELECT value, COUNT(*) AS count
                FROM sodsurvey LEFT OUTER JOIN age
                ON sodsurvey.age_id = age.id 
                WHERE value IS NOT NULL AND office_id = " . $office_id . " 
                AND month = " . $_SESSION['filtermonth'] . " 
                AND year = " . $_SESSION['filteryear'] . " 
                GROUP BY age_id; ";
            }

            $result = $dbh->prepare($sql);//prepares query  
            $result->execute();

@Alox这里是代码

$EX_SQL="";

if ($_SESSION['filteryear'] != 0 && $_SESSION['filtermonth'] != 0) {
    $EX_SQL=" AND month = " . $_SESSION['filtermonth'] . " AND year = " . $_SESSION['filteryear'] . "";
} else if($_SESSION['filteryear'] != 0) {
    $EX_SQL=" AND month = " . $_SESSION['filtermonth'] . "";
} else {
   $EX_SQL=" AND year = " . $_SESSION['filteryear'] . "";
}

        $sql = "SELECT value, COUNT(*) AS count
               FROM sodsurvey LEFT OUTER JOIN age
               ON sodsurvey.age_id = age.id 
               WHERE value IS NOT NULL AND office_id = " . $office_id . "
               ".$EX_SQL."
                GROUP BY age_id; ";

            $result = $dbh->prepare($sql);//prepares query  
            $result->execute();

3 个答案:

答案 0 :(得分:0)

将有3个案例。

  1. 您有两年 AND
  2. 您要么有月 OR 年。
  3. 你没有任何一个。
  4. 您当前的 PHP 代码仅支持2种情况。 (第1和第3个)你应该添加另一个 else if 块。

    为了更好地优化,您可以根据参数替换SQL字符串。像这样,

    $EX_SQL="";
    
    if ($_SESSION['filteryear'] != 0 && $_SESSION['filtermonth'] != 0) {
        $EX_SQL=" AND month = " . $_SESSION['filtermonth'] . " AND year = " . $_SESSION['filteryear'] . "";
    } else if($_SESSION['filteryear'] != 0) {
        $EX_SQL=" AND month = " . $_SESSION['filtermonth'] . "";
    } else {
       $EX_SQL=" AND year = " . $_SESSION['filteryear'] . "";
    }
    

    查询中的用户EX_SQL字符串如下:

    $sql = "SELECT value, COUNT(*) AS count FROM sodsurvey LEFT OUTER JOIN age ON sodsurvey.age_id = age.id WHERE value IS NOT NULL AND office_id=".$office_id." ".$EX_SQL." GROUP BY age_id; ";
    

    以上代码应涵盖所有3个案例。

答案 1 :(得分:0)

尝试以下代码

$sql = "SELECT value, COUNT(*) AS count
FROM sodsurvey LEFT OUTER JOIN age
ON sodsurvey.age_id = age.id 
WHERE value IS NOT NULL AND office_id = " . $office_id;

if(isset($_SESSION['filteryear']) && !empty($_SESSION['filteryear'])) {
    $sql . = "AND year = " . $_SESSION['filteryear']; 
}

if(isset($_SESSION['filtermonth']) && !empty($_SESSION['filtermonth'])) {
    $sql . = "AND month = " . $_SESSION['filtermonth']; 
}
$sql . = "GROUP BY age_id";


$result = $dbh->prepare($sql);//prepares query  
$result->execute();

答案 2 :(得分:0)

经过一些实验后,我找到了解决方案

        $filter = "";

        if ($_SESSION['filteryear'] != 0 && $_SESSION['filtermonth'] != 0) {
            $filter = "AND year = " . $_SESSION['filteryear'] . " AND month = " . $_SESSION['filtermonth'] . "";
        } else if ($_SESSION['filteryear'] != 0 || $_SESSION['filtermonth'] != 0) {
            $filter = "AND (year = " . $_SESSION['filteryear'] . " OR month = " . $_SESSION['filtermonth'] . ")";
        }

        $sql = "SELECT value, COUNT(*) AS count
        FROM sodsurvey LEFT OUTER JOIN age
        ON sodsurvey.age_id = age.id 
        WHERE value IS NOT NULL AND office_id = " . $office_id . "
        ".$filter."
        GROUP BY age_id; ";

感谢您指出我正确的方向