【发布时间】:2017-02-25 17:48:12
【问题描述】:
我有这个在 MySql 5 上完美运行的查询
'SELECT a.id, a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id'
基本上它从艺术家表中选择id、名称和日期,然后用国家表中的适当名称替换国家的id。
注意:artist.country_id 是外键,即 country.id。每个艺术家都属于某个国家,并且它的表中有一个 country_id。我需要为艺术家获取数据,但也需要将 country_id 替换为其适当的名称。我不想显示国家/地区的号码,我想显示国家/地区的名称。
$artists = $this->db->query('SELECT a.id, a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
我面临的问题是,artist.name 被 country.name 覆盖,我在结果中看不到艺术家姓名,只有国家/地区名称。
如何在 CodeIgniter 查询构建器下实现此功能?
我使用 CodeIgniter 版本 3。
编辑:添加了更多代码:
$this->db->select('artist.id', 'artist.name', 'artist.date', 'country.name', false);
$this->db->from('artist');
$this->db->join('country', 'country.id=artist.country_id');
$artists = $this->db->get();
部分结果:
[0] => stdClass Object
(
[id] => 1
[name] => Macedonia
[date] => 1979-10-10
)
[1] => stdClass Object
(
[id] => 2
[name] => Britain
[date] => 2003-01-01
)
结果中没有艺术家姓名。国家名称覆盖此字段。
【问题讨论】:
-
查找查询生成器的join方法
标签: php mysql codeigniter