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

减少仅按字段名顺序不同的mysql语句

  •  0
  • Chris  · 技术社区  · 16 年前

    我正在尝试按升序和降序的不同字段对数据进行排序。但是对于我拥有的4个字段(总共8个查询),我有不同的mysql pdo语句:

    $stmt1 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 DESC");
    $stmt2 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 ASC");
    $stmt3 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field2 DESC");
    $stmt4 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field3 ASC");
    $stmt5 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field3 DESC");
    $stmt6 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field3 ASC");
    $stmt7 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field4 DESC");
    $stmt8 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field4 ASC");
    

    基于输入,我选择正确的语句并绑定并执行它。

    if ($sortcode == 1){ 
       $stmt1->bindParam(':categ', $categ, PDO::PARAM_STR);
       $stmt1->execute();
       $fetched = $stmt1->fetchAll(PDO::FETCH_ASSOC);
    } else if ($sortcode == 2){
       $stmt2->bindParam(':categ', $categ, PDO::PARAM_STR);
       $stmt2->execute();
       $fetched = $stmt2->fetchAll(PDO::FETCH_ASSOC);
    } else if ($sortcode == 3){
       $stmt3->bindParam(':categ', $categ, PDO::PARAM_STR);
       $stmt3->execute();
       $fetched = $stmt3->fetchAll(PDO::FETCH_ASSOC);
    }
    //repeat the block 5 more times, for a total of 8
    

    这看起来一点都不对劲。由于select语句只在字段名和desc/asc之间存在差异,是否有更好的方法来获取 $sortcode 然后压缩下面的代码?

    我想我可以更具体地说这个问题:有没有一种方法可以让单个语句/单个PDO语句动态绑定字段名和asc/decs?

    3 回复  |  直到 16 年前
        1
  •  4
  •   novalis    16 年前

    使用关联数组保存准备好的语句。

    您的输入是一个列和排序方法,对吗?因此,通过以下方式准备查询:

    $columns = array("field1", "field2", "field3", "field4");
    $orders = array("asc", "desc");
    $queries = array();
    foreach($columns as $col) {
      $queries[$column] = array();
      foreach ($orders as $order) {
         $queries[$column][$order] = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY $column $order");
      }
    }
    

    现在,要查找正确的查询,您不需要一些合成代码——只需按列和顺序直接查找。

    要查找查询,不要让用户输入1-8之间的数字,而是让他们输入列和顺序。假设列在变量$col中,而顺序在$ord中。只需说$queries[$col][$ord]。

    如果出于某种原因你不得不使用这个号码(为什么?)然后你需要一个稍微不同的策略。在这种情况下,您存储查询 按那个数字 .

    $columns = array("field1", "field2", "field3", "field4");
    $orders = array("asc", "desc");
    $queries = array();
    $i = 0;
    foreach($columns as $col) {
      foreach ($orders as $order) {
         $i = $i + 1;
         $queries[$i] = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY $column $order");
      }
    }
    

    换句话说,您应该根据计划如何查找查询来存储查询。

        2
  •  1
  •   Monkey Boson    16 年前

    您可以按列号排序,列号可以参数化。如果在select子句中指定列,那么以这种方式使用order by通常会更清晰。我不确定asc/desc是否可以参数化,尽管…

        3
  •  0
  •   LukáÅ¡ Lalinský    16 年前

    您始终可以动态构建查询。将地图存储在 $sortcode 还有柱子, SELECT * FROM tabname WHERE categ=:categ 作为查询的一部分,获取基于 美元分类代码 并添加 ORDER BY ... 部分。

    推荐文章