目前我有外键thirdsmcontent_id值,现在我想得到
的值 thirdsubmenu_name
来自thirdmenu
表,submenu_name
来自submenu
表,menu_name
来自mainmenu
表。
请帮我解决这个问题。 我试过下面的代码,但我的模型代码不正确。它不显示我的数据
我的数据库表格结构
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
答案 0 :(得分:1)
您将FROM
和JOIN
定义为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
,了解如何定义联接。