我有两个不同的表,列名相同,名称为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;
}
答案 0 :(得分:2)
试试这个:
$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
的值答案 1 :(得分:0)
来自http://www.w3schools.com/sql/sql_union.asp
Blockquote UNION运算符用于组合两个或多个SELECT语句的结果集。请注意UNION中的每个SELECT语句必须具有相同的列数。列还必须具有类似的数据类型。此外,每个SELECT语句中的列必须具有相同的顺序。
在你的情况下它会是这样的:
SELECT columnname FROM tbl_specialty
UNION ALL
SELECT columnname FROM tbl_student;