gpt4 book ai didi

php - codeigniter:无法使用连接检索数据

转载 作者:行者123 更新时间:2023-11-29 00:32:51 24 4
gpt4 key购买 nike

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

thirdsubmenu_name 来自 thirdmenu 表,submenu_name 来自 submenu 表,menu_name 来自主菜单 表。

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

我的数据库表结构

   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 ......

在我的 Controller 中

 $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

最佳答案

您将 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了解如何定义连接。

关于php - codeigniter:无法使用连接检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com