如何将这些查询合并为一个

时间:2014-03-21 13:57:05

标签: mysql sql database-performance

所以,是的,我试图绕过这个问题,但我想不出一个解决方案。 首先,它没有任何损坏,但我对我的方式并不满意,我认为可能有更好的方法来实现这一目标。

好的我想要的是这个:

["id"] => string(1) "1"
["name"] => string(4) "name"
["type"] => string(4) "text"
["entity"] => string(4) "user"
["required"] => string(1) "1"
["sorting"] => string(2) "99"
["validator_id"] => string(1) "1"
["validator_name"] => string(12) "StringLength"
["filter_id"] => string(1) "1"
["filter_name"] => string(10) "StringTrim"
["form_field_id"] => string(1) "1"
["form_validator_id"] => string(1) "1"
["validator_options"] => string(20) "a:1:{s:3:"min";i:2;}"

我的所作所为:

$q = 'SELECT
            ff.*, fhff.sorting
        FROM
            form_field ff
        INNER JOIN form_has_form_field fhff ON fhff.form_field_id = ff.id
        WHERE
            ff.id IN (
                SELECT
                    fhff.form_field_id
                FROM
                    form_has_form_field fhff
                WHERE
                    fhff.form_id = "'.$id.'"
            )
        ORDER BY fhff.sorting ';

然后我在表单上执行选择以获取名称

现在我做一些检查,如果我有结果,合并清理它,附加形式名等...

结果如下:

["id"] => string(1) "2"
["name"] => string(4) "test"
["type"] => string(8) "textarea"
["entity"] => string(4) "user"
["required"] => string(1) "0"
["sorting"] => string(2) "70"

使用结果循环遍历此查询:

  $q = '
    SELECT
    form_validator.id as validator_id,
    form_validator.`name` as validator_name,
    form_filter.id as filter_id,
    form_filter.`name` as filter_name,
    form_field_has_form_validator.form_field_id,
    form_field_has_form_validator.form_validator_id,
    form_field_has_form_validator.`options` as validator_options
    FROM
    form_field
    INNER JOIN form_field_has_form_filter ON form_field_has_form_filter.form_field_id = form_field.id
    INNER JOIN form_field_has_form_validator ON form_field_has_form_validator.form_field_id = form_field.id
    INNER JOIN form_filter ON form_field_has_form_filter.form_filter_id = form_filter.id
    INNER JOIN form_validator ON form_field_has_form_validator.form_validator_id = form_validator.id
    HAVING form_field_id = "'.$r['id'].'"
';

其中$r['id']等于第一个结果数组的id

当我完成后,我合并了这些数组,它们就像我向你展示的第一个数组。

现在回到问题:

我可以以某种方式将这3个查询合并为1,这在性能方面是否会受益?

编辑:

下面是关于这些表如何连接的屏幕http://s14.directupload.net/images/140321/peailx3y.png http://i60.tinypic.com/nnmr9t.png http://s21.postimg.org/9bzr3f8dj/nnmr9t.png

还有一条附加信息:我在开头只有form.id,并尝试从表格的其他表中获取与form_field_table连接的所有信息

PS:OFC我正在使用PDO

2 个答案:

答案 0 :(得分:4)

好的,我会尽力回答这个问题。

首先,您可能不需要在结果上循环查询。您需要将这些结果添加到逗号分隔的字符串或具有该性质的字符串中,并使用IN将它们输入到您的SQL语句中注意:我甚至不打算使用此问题进入存在,所以不要问质疑观众。

其次不要想要在这种情况下使用HAVING子句,因为它在收集所有结果后应用了过滤器。为了表现,这在冰上很糟糕,并不是你想要做的事情。因此,应用这两个修复程序会使您的解决方案看起来像这样。

SELECT
    form_validator.id as validator_id,
    form_validator.`name` as validator_name,
    form_filter.id as filter_id,
    form_filter.`name` as filter_name,
    form_field_has_form_validator.form_field_id,
    form_field_has_form_validator.form_validator_id,
    form_field_has_form_validator.`options` as validator_options
FROM form_field
INNER JOIN form_field_has_form_filter 
    ON form_field_has_form_filter.form_field_id = form_field.id
INNER JOIN form_field_has_form_validator 
    ON form_field_has_form_validator.form_field_id = form_field.id
INNER JOIN form_filter 
    ON form_field_has_form_filter.form_filter_id = form_filter.id
INNER JOIN form_validator 
    ON form_field_has_form_validator.form_validator_id = form_validator.id
WHERE form_field.form_field_id IN (YourCommaSeperatedValueList)

接下来让我们谈谈格式化。由于表名有点乱,我们将使用别名使其更容易阅读。这应该有助于您在评论中提到的WHERE子句错误。

SELECT
    FV.id as validator_id,
    FV.`name` as validator_name,
    FF.id as filter_id,
    FF.`name` as filter_name,
    FFV.form_field_id,
    FFV.form_validator_id,
    FFV.`options` as validator_options
FROM form_field as F
INNER JOIN form_field_has_form_filter FH
    ON FH.form_field_id = F.id
INNER JOIN form_field_has_form_validator  FFV.
    ON FFV..form_field_id = F.id
INNER JOIN form_filter FF
    ON FH.form_filter_id = FF.id
INNER JOIN form_validator AS FV
    ON FFV.form_validator_id = FV.id
WHERE F.form_field_id IN (YourCommaSeperatedValueList)

看起来好多了,现在你的WHERE子句更容易正确,因为你没有重叠的列名。

现在最后但并非最不重要的是找到那些为这些桌子命名的人,并且直接在脸上打出

答案 1 :(得分:0)

这就是我最终所做的

SELECT
            form_field.*, form_has_form_field.sorting,
            form_validator.id AS validator_id,
            form_validator.`name` AS validator_name,
            form_filter.id AS filter_id,
            form_filter.`name` AS filter_name,
            form_field_has_form_validator.form_field_id,
            form_field_has_form_validator.form_validator_id,
            form_field_has_form_validator.`options` AS validator_options
        FROM
            form_field
        INNER JOIN 
            form_field_has_form_filter 
            ON form_field_has_form_filter.form_field_id = form_field.id
        INNER JOIN 
            form_field_has_form_validator 
            ON form_field_has_form_validator.form_field_id = form_field.id
        INNER JOIN
            form_filter 
            ON form_field_has_form_filter.form_filter_id = form_filter.id
        INNER JOIN 
            form_validator 
            ON form_field_has_form_validator.form_validator_id = form_validator.id
        INNER JOIN 
            form_has_form_field 
            ON form_has_form_field.form_field_id = form_field.id
        WHERE
            form_field.id IN (
                SELECT
                    form_has_form_field.form_field_id
                FROM
                    form_has_form_field
                WHERE
                    form_has_form_field.form_id = "'.$id.'"
            )
        ORDER BY
            form_has_form_field.sorting