SQL
1SELECT * FROM hotels 2 where (hotel_id, country_code) in ( 3 select hotel_id, country_code 4 from hotels_tags inner join tags 5 on hotels_tags.tags_id = tags.tags_id 6 where tag in ('タグ1', 'タグ2') 7 group by hotel_id, country_code 8 having count(*)=2 9 )
このようなSQL文以下のPHPのメソッド内でを作成したいです。
PHP
1 public function getHotelsWithTags($tags) 2 { 3 try { 4 $sql = "SQL文" 5 6 //処理 7 8 return $results; 9 } catch (PDOException $e) { 10 return $e ->getMessage(); 11 } 12 }
この変数$tagsには実際に検索に使いたいタグ名が入ってきます。必ず1個以上入ってくるようになっています。が何個入ってくるかはわかりません。
もしタグが3つであれば、PHP内で
PHP
1$sql = "SELECT * FROM hotels 2 where (hotel_id, country_code) in ( 3 select hotel_id, country_code 4 from hotels_tags inner join tags 5 on hotels_tags.tags_id = tags.tags_id 6 where tag in (?, ?, ?) 7 group by hotel_id, country_code 8 having count(*)=2 9 )"
のように$tagsの数に合わせて、(?,?)を挿入したいのですが、どのように書けば良いでしょうか。思いつきません。
$tagsをforで回し
php
1$temp =.?
のような感じで?を追加して、最後にSQLに挿入する。くらいしか思いつきません。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。