将mysql视图与列IF结合使用

时间:2014-06-21 05:29:08

标签: php codeigniter mysqli

我真的需要帮助和启发: - )

我有3个观点:

1st_view" vtabulasi_capaianbantu_fix":

    SELECT
    `a`.`tblpok_tahun` AS `tblpok_tahun`,
    `a`.`refsubunit_id` AS `refsubunit_id`,
    `a`.`tblpok_kodesubunit` AS `tblpok_kodesubunit`,
    `a`.`tblpok_kodeunit` AS `tblpok_kodeunit`,

IF (
    (`a`.`tblpok_targetkeu12` > 0),
    `a`.`tblpok_targetkeu12`,

IF (
    (`a`.`tblpok_targetkeu11` > 0),
    `a`.`tblpok_targetkeu11`,

IF (
    (`a`.`tblpok_targetkeu10` > 0),
    `a`.`tblpok_targetkeu10`,

IF (
    (`a`.`tblpok_targetkeu09` > 0),
    `a`.`tblpok_targetkeu09`,

IF (
    (`a`.`tblpok_targetkeu08` > 0),
    `a`.`tblpok_targetkeu08`,

IF (
    (`a`.`tblpok_targetkeu07` > 0),
    `a`.`tblpok_targetkeu07`,

IF (
    (`a`.`tblpok_targetkeu06` > 0),
    `a`.`tblpok_targetkeu06`,

IF (
    (`a`.`tblpok_targetkeu05` > 0),
    `a`.`tblpok_targetkeu05`,

IF (
    (`a`.`tblpok_targetkeu04` > 0),
    `a`.`tblpok_targetkeu04`,

IF (
    (`a`.`tblpok_targetkeu03` > 0),
    `a`.`tblpok_targetkeu03`,

IF (
    (`a`.`tblpok_targetkeu02` > 0),
    `a`.`tblpok_targetkeu02`,

IF (
    (`a`.`tblpok_targetkeu01` > 0),
    `a`.`tblpok_targetkeu01`,
    0
)
)
)
)
)
)
)
)
)
)
)
) AS `tblpok_targetkeu`,

IF (
    (
        `a`.`tblpok_targetfisik12` > 0
    ),
    `a`.`tblpok_targetfisik12`,

IF (
    (
        `a`.`tblpok_targetfisik11` > 0
    ),
    `a`.`tblpok_targetfisik11`,

IF (
    (
        `a`.`tblpok_targetfisik10` > 0
    ),
    `a`.`tblpok_targetfisik10`,

IF (
    (
        `a`.`tblpok_targetfisik09` > 0
    ),
    `a`.`tblpok_targetfisik09`,

IF (
    (
        `a`.`tblpok_targetfisik08` > 0
    ),
    `a`.`tblpok_targetfisik08`,

IF (
    (
        `a`.`tblpok_targetfisik07` > 0
    ),
    `a`.`tblpok_targetfisik07`,

IF (
    (
        `a`.`tblpok_targetfisik06` > 0
    ),
    `a`.`tblpok_targetfisik06`,

IF (
    (
        `a`.`tblpok_targetfisik05` > 0
    ),
    `a`.`tblpok_targetfisik05`,

IF (
    (
        `a`.`tblpok_targetfisik04` > 0
    ),
    `a`.`tblpok_targetfisik04`,

IF (
    (
        `a`.`tblpok_targetfisik03` > 0
    ),
    `a`.`tblpok_targetfisik03`,

IF (
    (
        `a`.`tblpok_targetfisik02` > 0
    ),
    `a`.`tblpok_targetfisik02`,

IF (
    (
        `a`.`tblpok_targetfisik01` > 0
    ),
    `a`.`tblpok_targetfisik01`,
    0
)
)
)
)
)
)
)
)
)
)
)
) AS `tblpok_targetfisik`,

IF (
    (
        `a`.`tblpok_realisasikeu12` > 0
    ),
    `a`.`tblpok_realisasikeu12`,

IF (
    (
        `a`.`tblpok_realisasikeu11` > 0
    ),
    `a`.`tblpok_realisasikeu11`,

IF (
    (
        `a`.`tblpok_realisasikeu10` > 0
    ),
    `a`.`tblpok_realisasikeu10`,

IF (
    (
        `a`.`tblpok_realisasikeu09` > 0
    ),
    `a`.`tblpok_realisasikeu09`,

IF (
    (
        `a`.`tblpok_realisasikeu08` > 0
    ),
    `a`.`tblpok_realisasikeu08`,

IF (
    (
        `a`.`tblpok_realisasikeu07` > 0
    ),
    `a`.`tblpok_realisasikeu07`,

IF (
    (
        `a`.`tblpok_realisasikeu06` > 0
    ),
    `a`.`tblpok_realisasikeu06`,

IF (
    (
        `a`.`tblpok_realisasikeu05` > 0
    ),
    `a`.`tblpok_realisasikeu05`,

IF (
    (
        `a`.`tblpok_realisasikeu04` > 0
    ),
    `a`.`tblpok_realisasikeu04`,

IF (
    (
        `a`.`tblpok_realisasikeu03` > 0
    ),
    `a`.`tblpok_realisasikeu03`,

IF (
    (
        `a`.`tblpok_realisasikeu02` > 0
    ),
    `a`.`tblpok_realisasikeu02`,

IF (
    (
        `a`.`tblpok_realisasikeu01` > 0
    ),
    `a`.`tblpok_realisasikeu01`,
    0
)
)
)
)
)
)
)
)
)
)
)
) AS `tblpok_realisasikeu`,

IF (
    (
        `a`.`tblpok_realisasifisik12` > 0
    ),
    `a`.`tblpok_realisasifisik12`,

IF (
    (
        `a`.`tblpok_realisasifisik11` > 0
    ),
    `a`.`tblpok_realisasifisik11`,

IF (
    (
        `a`.`tblpok_realisasifisik10` > 0
    ),
    `a`.`tblpok_realisasifisik10`,

IF (
    (
        `a`.`tblpok_realisasifisik09` > 0
    ),
    `a`.`tblpok_realisasifisik09`,

IF (
    (
        `a`.`tblpok_realisasifisik08` > 0
    ),
    `a`.`tblpok_realisasifisik08`,

IF (
    (
        `a`.`tblpok_realisasifisik07` > 0
    ),
    `a`.`tblpok_realisasifisik07`,

IF (
    (
        `a`.`tblpok_realisasifisik06` > 0
    ),
    `a`.`tblpok_realisasifisik06`,

IF (
    (
        `a`.`tblpok_realisasifisik05` > 0
    ),
    `a`.`tblpok_realisasifisik05`,

IF (
    (
        `a`.`tblpok_realisasifisik04` > 0
    ),
    `a`.`tblpok_realisasifisik04`,

IF (
    (
        `a`.`tblpok_realisasifisik03` > 0
    ),
    `a`.`tblpok_realisasifisik03`,

IF (
    (
        `a`.`tblpok_realisasifisik02` > 0
    ),
    `a`.`tblpok_realisasifisik02`,

IF (
    (
        `a`.`tblpok_realisasifisik01` > 0
    ),
    `a`.`tblpok_realisasifisik01`,
    0
)
)
)
)
)
)
)
)
)
)
)
) AS `tblpok_realisasifisik`,

IF (
    (
        `a`.`tblpok_realisasikeunominal12` > 0
    ),
    `a`.`tblpok_realisasikeunominal12`,

IF (
    (
        `a`.`tblpok_realisasikeunominal11` > 0
    ),
    `a`.`tblpok_realisasikeunominal11`,

IF (
    (
        `a`.`tblpok_realisasikeunominal10` > 0
    ),
    `a`.`tblpok_realisasikeunominal10`,

IF (
    (
        `a`.`tblpok_realisasikeunominal09` > 0
    ),
    `a`.`tblpok_realisasikeunominal09`,

IF (
    (
        `a`.`tblpok_realisasikeunominal08` > 0
    ),
    `a`.`tblpok_realisasikeunominal08`,

IF (
    (
        `a`.`tblpok_realisasikeunominal07` > 0
    ),
    `a`.`tblpok_realisasikeunominal07`,

IF (
    (
        `a`.`tblpok_realisasikeunominal06` > 0
    ),
    `a`.`tblpok_realisasikeunominal06`,

IF (
    (
        `a`.`tblpok_realisasikeunominal05` > 0
    ),
    `a`.`tblpok_realisasikeunominal05`,

IF (
    (
        `a`.`tblpok_realisasikeunominal04` > 0
    ),
    `a`.`tblpok_realisasikeunominal04`,

IF (
    (
        `a`.`tblpok_realisasikeunominal03` > 0
    ),
    `a`.`tblpok_realisasikeunominal03`,

IF (
    (
        `a`.`tblpok_realisasikeunominal02` > 0
    ),
    `a`.`tblpok_realisasikeunominal02`,

IF (
    (
        `a`.`tblpok_realisasikeunominal01` > 0
    ),
    `a`.`tblpok_realisasikeunominal01`,
    0
)
)
)
)
)
)
)
)
)
)
)
) AS `tblpok_realisasikeunominal`,
 `b`.`refsubunit_nama` AS `tblpok_uraian`
FROM
    (
        `vtblpok` `a`
        JOIN `refsubunit` `b` ON (
            (
                `b`.`refsubunit_id` = `a`.`refsubunit_id`
            )
        )
    )
WHERE
    (
        `a`.`tblpok_statusentry` = 'entryTargetRealisasi'
    )

2nd_view" vtabulasi_capaianbantu":

    SELECT
    `a`.`tblpok_tahun` AS `tblpok_tahun`,
    `a`.`refsubunit_id` AS `refsubunit_id`,
    `a`.`tblpok_kodesubunit` AS `tblpok_kodesubunit`,
    `a`.`tblpok_kodeunit` AS `tblpok_kodeunit`,

IF (
    (`a`.`tblpok_targetkeu` > 100),
    100,
    `a`.`tblpok_targetkeu`
) AS `tblpok_targetkeu`,

IF (
    (
        `a`.`tblpok_targetfisik` > 100
    ),
    100,
    `a`.`tblpok_targetfisik`
) AS `tblpok_targetfisik`,

IF (
    (
        `a`.`tblpok_realisasikeu` > 100
    ),
    100,
    `a`.`tblpok_realisasikeu`
) AS `tblpok_realisasikeu`,

IF (
    (
        `a`.`tblpok_realisasifisik` > 100
    ),
    100,
    `a`.`tblpok_realisasifisik`
) AS `tblpok_realisasifisik`,
 `a`.`tblpok_realisasikeunominal` AS `tblpok_realisasikeunominal`,
 `a`.`tblpok_uraian` AS `tblpok_uraian`
FROM
    `vtabulasi_capaianbantu_fix` `a`

3rd_view"得到平均值和总和":

    SELECT
    `a`.`refsubunit_id` AS `refsubunit_id`,
    `a`.`tblpok_kodesubunit` AS `tblpok_kodesubunit`,
    `a`.`tblpok_kodeunit` AS `tblpok_kodeunit`,
    `a`.`tblpok_uraian` AS `tblpok_namasubunit`,
    avg(`a`.`tblpok_realisasikeu`) AS `tblpok_realisasikeu`,
    sum(
        `a`.`tblpok_realisasikeunominal`
    ) AS `tblpok_realisasikeunominal`,
    (
        SELECT
            avg(`b`.`tblpok_realisasikeu`)
        FROM
            `vtabulasi_capaianbantu` `b`
    ) AS `tblpok_realisasikeu_allskpd`,
    (
        SELECT
            sum(`c`.`tblpok_apbdmurni`)
        FROM
            `vtabel_tblpok_level2_by_subunitid` `c`
    ) AS `tblpok_apbdmurni_allskpd`,
    (
        SELECT
            sum(`c`.`tblpok_apbdperubahan`)
        FROM
            `vtabel_tblpok_level2_by_subunitid` `c`
    ) AS `tblpok_apbdperubahan_allskpd`,
    (
        SELECT
            sum(
                `b`.`tblpok_realisasikeunominal`
            )
        FROM
            `vtabulasi_capaianbantu` `b`
    ) AS `tblpok_realisasikeunominal_allskpd`,
    `c`.`tblpok_apbdmurni` AS `tblpok_apbdmurni`,
    `c`.`tblpok_apbdperubahan` AS `tblpok_apbdperubahan`
FROM
    (
        `vtabulasi_capaianbantu` `a`
        JOIN `vtabel_tblpok_level2_by_subunitid` `c` ON (
            (
                `a`.`refsubunit_id` = `c`.`refsubunit_id`
            )
        )
    )
GROUP BY
    `a`.`refsubunit_id`

目前我已经使用过这个模型调用的视图:

模型:

$this->db->select("*");
        $query = $this->db->get('3rd_view');
        return $query->result_array(); 

一切都好。所有传递都很好,直到表的MVC->视图循环。 现在的问题是:

  1. 我无法改变视图查询" where子句"因为我需要在1st_view中查询此数据年度过滤器(atblpok_tahun)。任何意见/解决方案?
  2. 我无法控制1st_view IF迭代(12到1字段),因为我无法将php var传递给第一个视图查询。 (例如,如果我需要过滤从第1个月到第4个月的数据,在我的php表单中,已经有几个月的组合框,我选择4)。然后我需要限制嵌套的IF从1到4个嵌套。我们怎么做?
  3. 我担心使用许多组合视图会有很多性能下降,内部有很多IF过程。 (加载时间8s)
  4. 我应该找到另一种查询策略吗?它应该在mysql或php端进行大量编码吗?
  5. 感谢您的回复。

0 个答案:

没有答案