简体   繁体   English

Codeigniter在其中加入多行

[英]codeigniter join multiple rows where

I'm trying to code a search function for a site written on CodeIgniter but have trouble with the join statement in the model. 我正在尝试为在CodeIgniter上编写的站点编写搜索功能,但是模型中的join语句遇到了麻烦。

A controller passes an array of search criteria to the model function: get_sales($search_criteria); 控制器将搜索条件数组传递给模型函数:get_sales($ search_criteria);

function get_sales($search_criterie){

    $this->db->join('sales_prices', 'sales_prices.sale_id = sales.sale_id', 'left');
    $this->db->join('sales_sizes', 'sales_sizes.sale_id = sales.sale_id', 'left');
    $query = $this->db->get_where('sales', $search_criterie);
    $sale_data = $query->result_array();

}

I'm running into two problems: 我遇到两个问题:

1) I need a WHERE clause in the sales_sizes join. 1)我需要在sales_sizes连接中使用WHERE子句。 Right now it joins sales_sizes.sale_id ON sales.sale_id but I want to be able to filter by size via the $search_criteria array. 现在,它在sales.sale_id上​​加入了sales_sizes.sale_id,但我希望能够通过$ search_criteria数组按大小进行过滤。 How can I do this? 我怎样才能做到这一点?

2) Every sales usually has multiple sizes and I want the output of the query formatted like: 2)每个销售通常都有多个大小,我希望查询的输出格式如下:

Array
(
    [sale_id] => 1
    [sale_category] => shoes
    [sale_price] => 29.99
    [sale_sizes] => Array
        (
            [0] => 10
            [1] => 10.5
            [2] => 11

        )

)

I've tried some foreach loops to format the output but can't get it to work. 我尝试了一些foreach循环来格式化输出,但是无法使其正常工作。 Hope somebody can help. 希望有人能帮忙。

Update: 更新:

How do I process the following query result into a format like the one above? 如何将以下查询结果处理为上述格式?

Array
(
    [0] => Array
        (
            [sale_id] => 1
            [sale_category] => shoes
            [sale_price] => 29.99
            [sale_size] => 10
        )

    [1] => Array
        (
            [sale_id] => 1
            [sale_category] => shoes
            [sale_price] => 29.99
            [sale_size] => 10.5
        )

    [2] => Array
        (
            [sale_id] => 1
            [sale_category] => shoes
            [sale_price] => 29.99
            [sale_size] => 11    
        )

) 

I am personally not a big fan of using the active record as given. 我个人不喜欢使用给定的活动记录。 I find that it often restricts the programmer from writing more complex queries. 我发现它通常会限制程序员编写更复杂的查询。 If you find that this is true as well you could rewrite your query as followed: 如果您发现这也是对的,则可以按照以下方式重写查询:

$this->db->query('SELECT * FROM `sales_prices`, `sales_sizes` WHERE `sales_prices`.`sale_id` =   `sales`.`sale_id` AND `sales_sizes`.`sale_id` = `sales`.`sale_id` AND `sales`  = ?', array($search_criterie));
$sale_data = $query->results();

This will generate the same result and will also parameterize your query. 这将产生相同的结果,并将参数化您的查询。 Note that the question mark corresponds in order to values in your array that will make up the second parameter of you $this->db->query() function. 请注意,问号对应于数组中的值,这些值将构成$this->db->query()函数的第二个参数。

Try this type of query 试试这种类型的查询

$this->db->select('places.*, category.*')
            ->from('places')
            ->join('category', 'places.category_id = category.category_id', 'left')
            ->join('places_reviews', 'places_reviews.place_id = places.id', 'left')
            ->where('places.category_id', $category_id)
            ->limit($limit, $offset)
            ->order_by($sort_by, $sort_order);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM