我有 2 个不同的表,它们具有相同的列名名称 tbl_specialty 和 tbl_student。我做了一个 JOIN 语句来连接 2 个表,现在我想输出表 1 的列名和表 2 的列名,我该如何实现这一点?我只从一列中获得复制。 请帮忙。
我的观点
<?php
foreach ($delegates->result() as $row )
{?>
<tr>
<td><a href=""> <?php echo $row->name; ?></a></td>
<td class="center"><?php echo $row->job; ?></td>
<td class="center"><?php echo $row->name; ?></td>
<td class="center"><?php echo $row->workplace;?></td>
<td class="center"><?php echo $row->country_name; ?></td>
</tr>
我的模型
public function delegates_per_course() {
$this>db>select('tbl_student.name,tbl_student.workplace,tbl_student.job,tbl_student.dob,tbl_student.email,tbl_student.mobile,tbl_country.country_name,tbl_specialty.name,tbl_country.country_id,tbl_country.country_name');
$this->db->from('tbl_student');
$this->db->join('tbl_country','tbl_student.country_id=tbl_country.country_id');
$this->db->join('tbl_specialty','tbl_student.specialty_id=tbl_specialty.id');
$this->db->order_by("tbl_country.country_name");
$query = $this->db->get();
return $query;
}
最佳答案
试试这个:
$this->db->select('tbl_student.name as stud_name,tbl_student.workplace,tbl_student.job,tbl_student.dob,tbl_student.email,tbl_student.mobile,tbl_country.country_name,tbl_specialty.name as spec_name,tbl_country.country_id,tbl_country.country_name');
我更改了查询并为同名列创建别名,例如:
tbl_student.name as stud_name
tbl_specialty.name as spec_name
现在您可以使用别名正确引用它们。如果没有别名,tbl_specialty.name 会覆盖 tbl_student.name
的值关于mysql - 想要从具有相同列名的 2 个表中输出。使用代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41462648/