如何在mysql数据库和Fetch Records中加入两个表。

时间:2018-02-26 10:02:53

标签: php mysql codeigniter

我想联合计划和tag_category_banching表。我想从计划和tag_category_banching获得类似结果。 enter image description here enter image description here

Array
 (
 [0] => Array
   (
    [plan_id] => 3
    [plan_user_id] => 1
    [plan_title] => 3 Months
    [plan_validity] => 90
    [plan_desc] => 90 days
    [plan_activation] => 1
    [plan_status] => 1
    [banch_id] => 4
    [products_id] => 3
    [tag_cat_id] => 3
    [tag_cat_name] => 3 Months
    [nw_eb_mz_art_type] => 3
    [tag_cat_type] => 2
    [plan_price] => 200
)
[1] => Array
(
    [plan_id] => 4
    [plan_user_id] => 1
    [plan_title] => 1 Month
    [plan_validity] => 30
    [plan_desc] => 30 days.
    [plan_activation] => 1
    [plan_status] => 1
    [banch_id] => 3
    [products_id] => 3
    [tag_cat_id] => 4
    [tag_cat_name] => 1 Month
    [nw_eb_mz_art_type] => 3
    [tag_cat_type] => 2
    [plan_price] => 100
)
[2] => Array
(
    [plan_id] => 1
    [plan_user_id] => 1
    [plan_title] => 1 yr plan
    [plan_validity] => 365 days
    [plan_desc] => 365 days.
    [plan_activation] => 1
    [plan_status] => 1
    [banch_id] =>  
    [products_id] =>
    [tag_cat_id] => 
    [tag_cat_name] =>
    [nw_eb_mz_art_type] =>
    [tag_cat_type] =>
    [plan_price] =>
)

[3] => Array
(
    [plan_id] => 2
    [plan_user_id] => 1
    [plan_title] => 6 months plan
    [plan_validity] => 180 days
    [plan_desc] => 180 days.
    [plan_activation] => 1
    [plan_status] => 1
    [banch_id] =>  
    [products_id] =>
    [tag_cat_id] => 
    [tag_cat_name] =>
    [nw_eb_mz_art_type] =>
    [tag_cat_type] =>
    [plan_price] =>
)

在计划表中所有计划都没有在tag_category_banching表中找到然后加入后这些字段在tag_category_banching中不匹配,其数据是空白的,如下面的数组。

1 个答案:

答案 0 :(得分:0)

$query = $this->db->query("SELECT * FROM table-one JOIN table-two ON table-1-id = table-2-id");
return $query->result();

注意: - JOIN和ON是获取两个表数据的关键字

AND table-one和table-2是您需要的表

AND table-one-id和table-two-id是连接的两个表的列名