代码之家  ›  专栏  ›  技术社区  ›  Vecta

SQL多个WHERE子句问题

  •  0
  • Vecta  · 技术社区  · 14 年前

    我正在尝试修改这个modx片段,以便它接受从db返回的多个值,而不是默认值。

    默认情况下,tvtags只能设置为一个变量。我对它做了一些修改,使它分解成一个变量列表。我想查询每个变量的数据库,并返回与每个变量相关联的标记。但是,我有困难,因为我对SQL和PHP还比较陌生。

    我插入了$region,它可以工作,但我不确定如何为$countries变量添加更多的where子句。

    谢谢你的帮助!

    if (!function_exists('getTags')) {
    
        function getTags($cIDs, $tvTags, $days) {
    
            global $modx, $parent;
    
            $docTags = array ();
    
            $baspath= $modx->config["base_path"] . "manager/includes";
            include_once $baspath . "/tmplvars.format.inc.php";
            include_once $baspath . "/tmplvars.commands.inc.php";
    
            if ($days > 0) {
                $pub_date = mktime() - $days*24*60*60;
            } else {
                $pub_date = 0;
            }
    
            list($region, $countries) = explode(",", $tvTags);
    
            $tb1 = $modx->getFullTableName("site_tmplvar_contentvalues");
            $tb2 = $modx->getFullTableName("site_tmplvars");
            $tb_content = $modx->getFullTableName("site_content");
            $query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
            $query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
            $query .= " LEFT JOIN $tb_content tb_content ON stc.contentid=tb_content.id ";
            $query .= " WHERE stv.name='".$region."' AND stc.contentid IN (".implode($cIDs,",").") ";
            $query .= " AND tb_content.pub_date >= '$pub_date' ";
            $query .= " AND tb_content.published = 1 ";
            $query .= " ORDER BY stc.contentid ASC;";
    
            $rs = $modx->db->query($query);
            $tot = $modx->db->getRecordCount($rs);
            $resourceArray = array();
            for($i=0;$i<$tot;$i++)  {
                $row = @$modx->fetchRow($rs);
                $docTags[$row['contentid']]['tags'] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);   
            }
                if ($tot != count($cIDs)) {
                $query = "SELECT name,type,display,display_params,default_text";
                $query .= " FROM $tb2";
                $query .= " WHERE name='".$region."' LIMIT 1";
                $rs = $modx->db->query($query);
                $row = @$modx->fetchRow($rs);
                $defaultOutput = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);
                foreach ($cIDs as $id) {
                    if (!isset($docTags[$id]['tags'])) {
                        $docTags[$id]['tags'] = $defaultOutput;
                    }
                }
            }
                return $docTags;
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Sean Kelleher    14 年前

    你没有添加更多 WHERE 条款,您使用 AND S和 OR 在已经存在的WHERE子句中。我想在排队之后说 $query .= " WHERE stv.name = '".$region... 你投入

    foreach ($countries as $country)
    {
        $query .= "OR stv.name = '{$country}', ";
    }
    

    但我不知道您希望查询如何工作。