[英]codeigniter: unable to retrieve the data using join
Currently i have foreign key thirdsmcontent_id value , now i want to get the value of 目前我有外键thirdsmcontent_id值,现在我想获取的值
thirdsubmenu_name
from thirdmenu
table, submenu_name
from submenu
table, menu_name
from mainmenu
table . thirdsubmenu_name
从thirdmenu
表, submenu_name
从submenu
表, menu_name
从mainmenu
表。
Please help me to solve this problem. 请帮我解决这个问题。 I tried below code but my model code is not correct.
我尝试了下面的代码,但是我的模型代码不正确。 it is not display my data
它不显示我的数据
My Database Table Structure 我的数据库表结构
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 ......
In My controller 在我的控制器中
$thirdsubmenu_id = $this->uri->segment(4);
$data['main_menuname'] = $this->thirdsmcontentdetailsmodel->getMainMenuNameOfSubmenu($thirdsubmenu_id);
In My model 在我的模型中
//---------------------------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');
}
Getting Error: 出现错误:
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
You are defining both FROM
and JOIN
as thirdsubmenu
that isn't possible and makes no sense. 您正在将
FROM
和JOIN
定义为不可能且没有意义的thirdsubmenu
。 Either use: 可以使用:
$this->db->join('thirdsubmenu','thirdsubmenu.submenu_id = thirdsmcontentdetails.submenu_id');
$query = $this->db->get_where('thirdsmcontentdetails',array('thirdsubmenu_id'=>$thirdsubmenu_id));
Or: 要么:
$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();
Have a look at the CI documentation
to see how you have to define a join. 查看
CI documentation
以了解如何定义联接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.