1

我有这个查询

SELECT
    arl_kind,
    IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
    arl_display_nr
FROM
    tof_art_lookup
LEFT JOIN
    tof_brands
ON
    bra_id = arl_bra_id
INNER JOIN
    tof_articles
ON
    tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
    tof_suppliers
ON
    tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
    arl_art_id = @art_id
AND arl_kind IN (3)
ORDER BY
    arl_kind,
    bra_brand,
    arl_display_nr limit 100;

当我在 phpmyadmin 或任何具有 sql 命令功能的软件中执行此操作时,一切正常并且输出是我应该的。

当我尝试在codeigniter中插入它时出现问题,我的主要问题是if语句,我试图像这样插入它

return $query = $this->db
    ->SELECT('
        arl_kind,
        IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
        arl_display_nr
    FROM
        tof_art_lookup
    LEFT JOIN
        tof_brands
    ON
        bra_id = arl_bra_id
    INNER JOIN
        tof_articles
    ON
        tof_articles.art_id = tof_art_lookup.arl_art_id
    INNER JOIN
        tof_suppliers
    ON
        tof_suppliers.sup_id = tof_articles.art_sup_id
    WHERE
        arl_art_id = @art_id
    AND arl_kind IN (3)
    ORDER BY
        arl_kind,
        bra_brand,
        arl_display_nr limit 100')
        ->get();

和第二个这样的选择

return $query = $this->db
    ->select('ARL_KIND, IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,ARL_DISPLAY_NR')
    ->from('tof_art_lookup')
    ->join('tof_brands','bra_id = arl_bra_id','LEFT')
    ->join('tof_articles','tof_articles.art_id = tof_art_lookup.arl_art_id','INNER')
    ->join('tof_suppliers','tof_suppliers.sup_id = tof_articles.art_sup_id','INNER')
    ->where('ARL_ART_ID',$id)
    ->where_in('ARL_KIND',array('3'))
    //->where('A.ARL_DISPLAY','0')
    ->group_by('arl_kind','bra_brand','arl_display_nr')
    ->get();

它们都可以工作,但如果没有第二个选择选项(带有 if 的选项),它们都可以工作。

谁能帮我解决这个问题。

4

2 回答 2

0

像这样使用

 $query_string = "SELECT
        arl_kind,
        IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
        arl_display_nr
    FROM
        tof_art_lookup
    LEFT JOIN
        tof_brands
    ON
        bra_id = arl_bra_id
    INNER JOIN
        tof_articles
    ON
        tof_articles.art_id = tof_art_lookup.arl_art_id
    INNER JOIN
        tof_suppliers
    ON
        tof_suppliers.sup_id = tof_articles.art_sup_id
    WHERE
        arl_art_id = @art_id
    AND arl_kind IN (3)
    ORDER BY
        arl_kind,
        bra_brand,
        arl_display_nr limit 100";



$query=$this->db->query($query_string);
if ($query->num_rows () > 0) {
    return $query->result_array ();
} else {
    return FALSE;
}
于 2013-08-26T06:44:16.393 回答
0

我相信你的代码应该是这样的

return $query = $this->db
        ->select('arl_kind, IF(tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,ARL_DISPLAY_NR', FALSE)
        ->from('tof_art_lookup')
        ->join('tof_brands', 'bra_id = arl_bra_id', 'LEFT')
        ->join('tof_articles', 'tof_articles.art_id  = tof_art_lookup.arl_art_id', 'INNER')
        ->join('tof_suppliers','tof_suppliers.sup_id = tof_articles.art_sup_id', 'INNER')
        ->where('arl_art_id', $id)
        ->where_in('arl_kind', array('3'))
        ->order_by('arl_kind','bra_brand','arl_display_nr')
        ->limit(100)
        ->get();

注意:select()方法的第二个参数设置为FALSEorder_by()使用,而不是group_by()在原始查询中使用。

于 2013-08-25T22:33:47.660 回答