codeigniter 表连接 [英] codeigniter table join
问题描述
我想在 1 个表中同时显示 user 表和 users_profiles 表:我想将它们链接起来,以便 usrpID = usrID,
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.
控制器:
$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; ?>
但是当我尝试连接两个表时,它返回一个错误:这是我的代码
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 表包含用户名、密码等字段,每个用户在 users_profiles 表中都有自己的个人资料
users table has fields like username,password, etc. and every user has his own profile in users_profiles table
users users_profiles
EDIT 我尝试选择字段,但是当我尝试此操作时
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
推荐答案
users
表同时在 from
和 join
函数中,所以在总和您加入了 3 个表:users
、users
和 users_profiles
-> 第 2 个具有相同的名称 -> error unique/别名表.
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.
试试这个(加入from
中的[users
] on [users_profiles
in join
]):>
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();
示例:
获取users_profiles
userpNick
列:
$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; ?>
这篇关于codeigniter 表连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!