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 shell s ON s.shop_id = a.shop_id INNER JOIN location l ON l.location_id = s.location_id INNER ON 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 に答える