Sql 如何在CodeIgniter中从具有相同列名的表中输出数据?

Sql 如何在CodeIgniter中从具有相同列名的表中输出数据?,sql,database,codeigniter,Sql,Database,Codeigniter,这是我的疑问: $query = $this->db->query(' SELECT archives.id, archives.signature, type_of_source.description, media_type.description, origin.description FROM archives, type_of_source, media_type, origin

这是我的疑问:

$query = $this->db->query('
    SELECT archives.id, archives.signature, type_of_source.description, media_type.description, origin.description 
    FROM archives, type_of_source, media_type, origin                                                             
    WHERE archives.type_of_source_id = type_of_source.id                                                          
    AND type_of_source.media_type_id = media_type.id                                                              
    AND archives.origin_id = origin.id                                                                            
    ORDER BY archives.id ASC
');
但是如何输出结果呢?此操作有效,但仅获取最后一个描述(origin.description):

这不起作用:

foreach ($query->result_array() as $row)
{
    echo $row['type_of_source.description'];
}

或者我应该重命名这些列(例如,type\u of\u source\u description)?

这实际上与CodeIgniter关系不大,而与mysql\u fetch\u assoc如何提供查询结果关系很大。 解决方案是,您应该使用“AS”重命名查询中的列,例如


只需在SELECT语句中为列添加别名。不要修改您的数据库。标准做法是在SQL语句中使用别名

从PostgreSQL的文档中选择:

使用该子句作为输出名称,可以为输出列指定另一个名称


我建议不要使用这种方式,相反,您可以使用postgres将输出列重命名为“AS”。

使用postgres,它仍然是一样的吗?我看不出为什么不应该使用“AS”这一标准SQL语法,我敢打赌PHP的pg_*函数与mysql_*函数非常相似。在我的例子中,我有很多没有AS的查询,重命名它们会非常痛苦。有没有办法做到这一点?
foreach ($query->result_array() as $row)
{
    echo $row['type_of_source.description'];
}
select type_of_source.description as type_of_source_description, origin.descripotion as origin_descripotion from ....