Php codeigniter:无法使用联接检索数据
目前我有外键thirdsmu id值,现在我想得到Php codeigniter:无法使用联接检索数据,php,mysql,codeigniter,join,Php,Mysql,Codeigniter,Join,目前我有外键thirdsmu id值,现在我想得到 thirdsubmenu\u namefromthirdmenutable,submenu\u namefromsubmenutable,menu\u namefrommain menutable 请帮我解决这个问题。 我尝试了下面的代码,但我的型号代码不正确。它不显示我的数据 我的数据库表结构 1)Table: mainmenu --------------- mainmenu_id PK(primary key)
thirdsubmenu\u name
fromthirdmenu
table,submenu\u name
fromsubmenu
table,menu\u name
frommain menu
table
请帮我解决这个问题。
我尝试了下面的代码,但我的型号代码不正确。它不显示我的数据
我的数据库表结构
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
您将
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->join('thirdsubmenu','thirdsubmenu.submenu_id = thirdsmcontentdetails.submenu_id');
$query = $this->db->get_where('thirdsmcontentdetails',array('thirdsubmenu_id'=>$thirdsubmenu_id));
或:
请查看以了解如何定义联接。我认为您在“选择此处”中使用了错误的表。@KishorSubedi您能为我的问题发布一个答案吗。你是尼泊尔人吗?是的,我是。好的,我会发布我的答案。我想你在“选择这里”中使用了错误的表格。@KishorSubedi你能发布我问题的答案吗。你是尼泊尔人吗?是的,我是。好的,我会发布我的答案。