1

虽然我之前问过一个与此类似的问题,但我尝试将相同的技术应用于它,但它只是没有按应有的方式工作,并且出现错误和各种问题。

我为此创建了一个 sqlfiddle;http://sqlfiddle.com/#!2/b1a29

我正在尝试创建一个选择函数,它将返回 animal_id、animal_name、animal_type_name、shelter_name、animal_type_id 和 location_name。

我试图用下面的代码来擅长它,但显然我错过了一些东西;

$query = $this->db->query('SELECT animal_id, animal_name, animal_type_name, shelter_name, shop_id, location_name
                                FROM animals a
                                INNER JOIN shelter s ON s.shop_id = a.shop_id
                                INNER JOIN location l ON l.location_id = s.location_id
                                INNER JOIN animal_types at ON at.animal_type_id = a.animal_type_id');
4

2 回答 2

2

问题是您选择的列名不明确或存在于多个表中。在这种情况下,它是shop_id. 为了很好地执行查询,您需要指定列shop_id应该来自哪里,

SELECT animal_id, animal_name, animal_type_name, 
       shelter_name, s.shop_id, location_name
FROM animals a
INNER JOIN shelter s ON s.shop_id = a.shop_id
INNER JOIN location l ON l.location_id = s.location_id
INNER JOIN animal_types at ON at.animal_type_id = a.animal_type_id
于 2013-02-27T03:24:43.493 回答
0

当我运行您的代码时,它会生成错误:

字段列表中的列“shop_id”不明确:SELECT animal_id、animal_name、animal_type_name、shelter_name、shop_id、location_name FROM animals a INNER JOIN shutter s ON s.shop_id = a.shop_id INNER JOIN location l ON l.location_id = s.location_id INNER在 at.animal_type_id = a.animal_type_id 处加入 animal_types

从错误中:您的shop_id列属于多个表,因此您必须使用表名/表别名对其进行限定。

将您的查询更改为:

SELECT animal_id, animal_name, animal_type_name, shelter_name, s.shop_id, location_name
                                FROM animals a
                                INNER JOIN shelter s ON s.shop_id = a.shop_id
                                INNER JOIN location l ON l.location_id = s.location_id
                                INNER JOIN animal_types at ON at.animal_type_id = a.animal_type_id
于 2013-02-27T03:25:50.597 回答