在php中优化和美化冗长而丑陋的SQL查询

时间:2015-07-16 06:00:16

标签: php mysql optimization query-optimization

我是php的初学者,我有sql字符串优化和美容的问题。

$kiti_neplp = mysql_query("SELECT (SELECT coalesce(SUM(skaicius)*6, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V3')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V4')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V7')
   +(SELECT coalesce(SUM(skaicius)*8, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V9')
   +(SELECT coalesce(SUM(skaicius)*0.3, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V11')
   +(SELECT coalesce(SUM(skaicius)*0.3, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V13')
   +(SELECT coalesce(SUM(skaicius)*16, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V14')
   +(SELECT coalesce(SUM(skaicius)*8, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V16')
   +(SELECT coalesce(SUM(skaicius)*8, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V17')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V18')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V19')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V21')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V22')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V23')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V24')
   +(SELECT coalesce(SUM(skaicius)*3, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V51')
   +(SELECT coalesce(SUM(skaicius)*4, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V52')
   +(SELECT coalesce(SUM(skaicius)*16, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V34')
   +(SELECT coalesce(SUM(skaicius)*16, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V35')
   +(SELECT coalesce(SUM(skaicius)*8, 0) FROM menesiai WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V36') 
    as kiti_neplp");

我正在建立SQL查询字符串,但它看起来真的很难看。这是建立这个冗长而丑陋的查询的更好方法吗?因为现在我写了10个几乎相同的字符串(具有不同的系数和rdkodas)。当然我可以写它并且它对我有用,但我想学习如何以一种好的方式做到这一点。 感谢您的理解

4 个答案:

答案 0 :(得分:0)

你可以试试这个

SELECT 
  IF (rdkodas = 'V3', coalesce(SUM(skaicius)*6, 0), 0) AS v3_sum,
  IF (rdkodas = 'V4', coalesce(SUM(skaicius)*4, 0) AS v4_sum,
  IF (rdkodas = 'V7', coalesce(SUM(skaicius)*4, 0) AS v7_sum
FROM menesiai
WHERE metai = '".$metaiat."' {$SQLmenuo} {$SQLskyrius} AND rdkodas IN ('V3', 'V4', 'V7')

答案 1 :(得分:0)

为什么你最好保存乘数(4)?

SUM(skaicius) * **4**

将数字保存在乘数列中,并使用类似

的查询进行查询
SUM(skaicius) * multiplier

您可以缩小rdkodas的查询

rdkodas in ('V4', 'V16', etc)

答案 2 :(得分:0)

如果你不能从这个数据中创造一个很好的方法来创建一个数学模式,我能想到的唯一真实的事情是创建一个数组并将它传递给一个为你构建查询字符串的函数,这样你就不会甚至不得不看到它。

$nums = array(6, 0, 3, //the numbers and data you have
              4, 0, 4,
              //snip
              8, 0, 36);


function createQuery($nums) {
    $qs = "SELECT ";
    for ($i = 0; $i < count($nums); $i+= 3) { //iterating over the groups of three
        $qs = $qs . "+(SELECT coalesce(SUM(skaicius)*" . $nums[$i] .
                ", " . $nums[$i + 1] . 
                ") FROM menesiai WHERE metai = '" . $metaiat . 
                "' {$SQLmenuo} {$SQLskyrius} AND rdkodas = 'V" . 
                 $nums[$i + 2] . "')+";
    }
    return $qs = $qs . "as kiti_neplp"; //return everything as a string
}

$kiti_nlpl = mysqli_query(createQuery($nums)); //execute the query

不要按原样运行此代码!在将其部署到SQL数据库之前对其进行测试。您可以在php页面上echo createQuery($nums);查看您将要进行的查询,以及是否与上述查询匹配。

这种方法从长远来看会对你有所帮助,特别是如果你要做更多的这些(不寒而栗)。在将num传递给函数之前,您可以声明$ nums中包含不同的值。这样做还有一个额外的好处,就是让实际执行查询的代码部分(mysqli_quer();)看起来非常好,通过隐藏那些你不必看到它的蹩脚部分:P

答案 3 :(得分:0)

SQL格式化程序SQLinForm 从PHP中获取SQL语句,美化它们并更好地重写PHP代码