1

目前我有外键 thirdsmcontent_id 值,现在我想获取的值

thirdsubmenu_namethirdmenu表,submenu_namesubmenu表,menu_namemainmenu表。

请帮我解决这个问题。我尝试了下面的代码,但我的模型代码不正确。它不显示我的数据

我的数据库表结构

   1)Table: mainmenu
    ---------------
     mainmenu_id   PK(primary key)
     menu_name     ..... 

    2)Table: submenu
    -------------------
     submenu_id     PK
     mainmenu_id    FK (foreign key refrences mainmenu table)
     submenu_name   ..... 


    3)Table: thirdsubmenu
    --------------------
      thirdsubmenu_id     PK
      submenu_id          FK (foreign key refrences submenu table)
      thirdsubmenu_name     ........


    4)Table: thirdsmcontentdetails
    --------------------
      thirdsmcontent_id   PK
      thirdsubmenu_id     FK (foreign key refrences thirdsubmenu table)
      content              ......

在我的控制器中

 $thirdsubmenu_id = $this->uri->segment(4);

   $data['main_menuname'] = $this->thirdsmcontentdetailsmodel->getMainMenuNameOfSubmenu($thirdsubmenu_id); 

在我的模型中

 //---------------------------get Main Menu Name by Menu id-----------------------------------
 function getMainMenuNameOfSubmenu($thirdsubmenu_id)
  {     
         $this->load->database();
         $query = $this->db->join('thirdsubmenu','thirdsubmenu.submenu_id = thirdsmcontentdetails.submenu_id')->get_where('thirdsubmenu',array('thirdsubmenu_id'=>$thirdsubmenu_id));  
         return $query->row('menu_name'); 
  }

得到错误:

    A Database Error Occurred

    Error Number: 1066

    Not unique table/alias: 'thirdsubmenu'

    SELECT * FROM (`thirdsubmenu`) JOIN `thirdsubmenu` ON `thirdsubmenu`.`submenu_id` = `thirdsmcontentdetails`.`submenu_id` WHERE `thirdsubmenu_id` = '1'

    Filename: D:\xampp\htdocs\system\database\DB_driver.php

    Line Number: 330
4

1 回答 1

1

您正在定义两者FROMJOIN因为thirdsubmenu这是不可能的,也没有任何意义。要么使用:

$this->db->join('thirdsubmenu','thirdsubmenu.submenu_id = thirdsmcontentdetails.submenu_id');
$query = $this->db->get_where('thirdsmcontentdetails',array('thirdsubmenu_id'=>$thirdsubmenu_id));

或者:

$this->db->from('thirdsmcontentdetails');
$this->db->join('thirdsubmenu','thirdsubmenu.submenu_id = thirdsmcontentdetails.submenu_id')
$this->db->where(array('thirdsubmenu_id'=>$thirdsubmenu_id));
$query = $this->db->get();

看看CI documentation你必须如何定义一个连接。

于 2013-03-31T12:43:09.097 回答