mysql - 想要从具有相同列名的 2 个表中输出。使用代码点火器

标签 mysql codeigniter codeigniter-2 codeigniter-3

我有 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/

相关文章:

php - 如何将 mysql 表中的两行连接到第三个表中

javascript - 如何在codeigniter中将2个或更多数据传递给ajax

php - CodeIgniter 3 中重定向后 Flashdata 未清除

php - Codeigniter 查询不插入

codeigniter - 使用 PHPUnit 测试 CodeIgniter 2.1 Controller

codeigniter - 如果输入文本为空,如何传递零?

c# - 如何在不卡住 UI 的情况下删除大量行?

PHP 行数和列数

javascript - 使用 BIOSTALL google map library 在 Code igniter 中设置 Google map pans

php - 我的结果页面的最后部分有问题