codeigniter 表连接

发布于 2024-11-08 10:55:08 字数 1633 浏览 0 评论 0原文

我想在 1 个表中显示用户表和 users_profiles 表: 我想将它们链接起来,以便 usrpID = usrID,

在此过程之前,我尝试使用此代码仅显示用户表,效果很好。

控制器:

$data['query'] = $this->db->query('SELECT * FROM users_profiles');
$this->load->view('users/users_view',$data);

视图:

<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo $row['usrID']</td>
            <td><?php echo $row['usrName'];?></td>
        </tr>
<? endforeach; ?>

但是当我尝试连接两个表时,它返回一个错误:这是我的代码

$this->db->select('users.usrID, users_profiles.usrpID');
$this->db->from('users', 'users_profiles');
$this->db->join('users', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();

用户表具有用户名、密码等字段,并且每个用户在 users_profiles 表中都有自己的个人资料

users           users_profiles

用户表users_profiles 表

编辑 我尝试选择字段,但是当我尝试这样做时,

<td><?php echo $row['usrID'];?></td>
            <td><?php echo $row['usrName'];?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>    

它返回了用户配置文件中不应该出现的第一个值

I want to display both the user table and users_profiles table in 1 table :
I want to link them both so that usrpID = usrID,

Before this process I tried displaying only users table using this code and it works great.

Controller:

$data['query'] = $this->db->query('SELECT * FROM users_profiles');
$this->load->view('users/users_view',$data);

View:

<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo $row['usrID']</td>
            <td><?php echo $row['usrName'];?></td>
        </tr>
<? endforeach; ?>

but when I try to join two tables, it returns me an error: this is my code

$this->db->select('users.usrID, users_profiles.usrpID');
$this->db->from('users', 'users_profiles');
$this->db->join('users', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();

users table has fields like username,password, etc. and every user has his own profile in users_profiles table

users           users_profiles

users tableusers_profiles table

EDIT I tried selecting the fields but when I tried this

<td><?php echo $row['usrID'];?></td>
            <td><?php echo $row['usrName'];?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>    

it returns me the first value in users profiles in which it should not

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2024-11-15 10:55:08

users 表位于 fromjoin 函数中,因此您总共连接了 3 个表:users、< code>users 和 users_profiles ->第 2 个具有相同的名称 -> 错误唯一/别名表

from中的[users]):

$this->db->select('users.usrID, users_profiles.usrpID')
         ->from('users')
         ->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();

编辑:

示例:

试试这个(在join]中的[users_profiles]上加入[ 获取users_profilesuserpNick列:

$this->db->select('users.usrID, users_profiles.userpNick')
         ->from('users')
         ->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$query = $this->db->get();

查看:

<?php foreach($query->result() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo $row->usrID</td>
            <td><?php echo $row->userpNick;?></td>
        </tr>
<? endforeach; ?>

users table was in both from and join functions, so in sum you were joining 3 tables: users, users and users_profiles -> the 2 first have the same name -> error unique/alias table.

Try this (joining [users in from] on [users_profiles in join]):

$this->db->select('users.usrID, users_profiles.usrpID')
         ->from('users')
         ->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();

EDIT:

example:

To get users_profiles userpNick column:

$this->db->select('users.usrID, users_profiles.userpNick')
         ->from('users')
         ->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$query = $this->db->get();

view:

<?php foreach($query->result() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo $row->usrID</td>
            <td><?php echo $row->userpNick;?></td>
        </tr>
<? endforeach; ?>
琴流音 2024-11-15 10:55:08

您可以参考员工表和地址表之间的连接结果示例。

function getEmployees(){
  $this->db->select("trn_employee.EMPLOYEE_ID,trn_employee.FIRST_NAME,trn_employee.LAST_NAME,trn_employee.EMAIL,trn_address.ADDRESS_LINE,trn_address.CITY");
  $this->db->from('trn_employee');
  $this->db->join('trn_address', 'trn_address.employee_id = trn_employee.employee_id');
  $query = $this->db->get();
  return $query->result();
 }

You can refer this example of join result between employee and address table.

function getEmployees(){
  $this->db->select("trn_employee.EMPLOYEE_ID,trn_employee.FIRST_NAME,trn_employee.LAST_NAME,trn_employee.EMAIL,trn_address.ADDRESS_LINE,trn_address.CITY");
  $this->db->from('trn_employee');
  $this->db->join('trn_address', 'trn_address.employee_id = trn_employee.employee_id');
  $query = $this->db->get();
  return $query->result();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文