sql从连接中获取数据并将结果作为关联数组获取

时间:2014-12-04 06:07:20

标签: php mysql codeigniter

 TABLE WORKSHOPS
--------------------------------
id|course_id|country_id|city_id|
--------------------------------
1 |  5      |    1     |   2   |
--------------------------------
2 |  5      |    1     |   4   |
--------------------------------
3 |  5      |    2     |   5   |
--------------------------------


TABLE WORKSHOP_DATES
----------------------------------------
id|start_date | end_date | workshop_id |
----------------------------------------
1 |2014-11-05 |2014-11-06|      1      |
----------------------------------------
2 |2014-11-28 |2014-11-29|      1      |
----------------------------------------
3 |2014-11-28 |2014-11-30|      1      |
----------------------------------------

我使用以下查询

$this->db->select("$this->_table.*, $_courses_table.name as course, $_countries_table.name as country, $_cities_table.name as city, $_workshop_dates_table.id as workshop_dates_id, $_workshop_dates_table.start_date as start_date, $_workshop_dates_table.end_date as end_date");


    $this->db->join("$_courses_table", "$_courses_table.id = $this->_table.ktw_course_id");
    $this->db->join("$_countries_table", "$_countries_table.id = $this->_table.ktw_country_id");
    $this->db->join("$_cities_table", "$_cities_table.id = $this->_table.ktw_city_id");
    $this->db->join("$_workshop_dates_table", "$_workshop_dates_table.ktw_workshop_id = $this->_table.id");        
    $this->db->where('ktw_course_id', $course_id);

    $this->db->group_by("$_workshop_dates_table.ktw_workshop_id");
    $this->db->order_by('start_date', 'ASC');

    return  $this->db->get($this->_table);

但结果是:

Array
(
    [0] => stdClass Object
        (
            [id] => 7
            [price] => 1300
            [discount] => 13
            [early_bird_date] => 2014-12-30
            [timing] => 
            [batch] => 
            [slug] => 
            [status] => 1
            [ktw_course_id] => 5
            [ktw_country_id] => 2
            [ktw_city_id] => 5
            [ktw_timeslot_id] => 0
            [ktw_training_mode_id] => 3
            [ktw_currency_id] => 6
            [created] => 
            [modified] => 
            [course] => jquery
            [country] => kuwait
            [city] => dubai
            [workshop_dates_id] => 7
            [start_date] => 2014-11-04
            [end_date] => 2015-01-05
        )

    [1] => stdClass Object
        (
            [id] => 5
            [price] => 900
            [discount] => 13
            [early_bird_date] => 2014-11-26
            [timing] => 
            [batch] => 
            [slug] => 
            [status] => 0
            [ktw_course_id] => 5
            [ktw_country_id] => 1
            [ktw_city_id] => 2
            [ktw_timeslot_id] => 0
            [ktw_training_mode_id] => 1
            [ktw_currency_id] => 1
            [created] => 
            [modified] => 2014-11-14 14:34:29
            [course] => jquery
            [country] => india
            [city] => vizag
            [workshop_dates_id] => 2
            [start_date] => 2014-11-28
            [end_date] => 2014-11-29
        )

但我需要这样(所有workshop_dates必须来到阵列中的特定workshop_id)

array(

    [0] => stdClass Object
        (
            [id] => 6
            [price] => 1300
            [discount] => 13
            [early_bird_date] => 2014-12-30
            [timing] => 
            [batch] => 
            [slug] => 
            [status] => 0
            [ktw_course_id] => 5
            [ktw_country_id] => 1
            [ktw_city_id] => 4
            [ktw_timeslot_id] => 0
            [ktw_training_mode_id] => 2
            [ktw_currency_id] => 1
            [created] => 
            [modified] => 2014-11-25 10:16:58
            [course] => jquery
            [country] => india
            [city] => hyderabad

            [workshop_dates] => array ( [0]

                                  [start_date] => 2014-12-30
                                  [end_date] => 2014-12-31

                                [1]
                                  [start_date] => 2014-11-28 
                                  [end_date] => 2014-12-29
                                 [2]
                                  [start_date] => 2014-11-28
                                    [end_date] => 2014-12-30
                                )

)

我希望它作为关联数组,一个课程可能有多个研讨会,同时它可能有多个日期

1 个答案:

答案 0 :(得分:0)

你必须使用2个查询

首先获得工作车间,并根据研讨会获得workshop_dates

然后使用foreach,你可以创建一个关联数组。