0

Tour テーブルと Tour Dates テーブルの 2 つのテーブルがあります。

  1. ツアーテーブル
    • ID
    • tour_title

ツアーテーブルを表示するためのテーブルのリンクは次のとおりです。

  1. tour_dates テーブル
    • ID
    • tour_id
    • 価格

tour_date テーブルを表示するためのテーブルのリンクは次のとおりです。

今、私は次のような価格帯のフィルターを使用しています

  • $10 から $50 まで
  • $50 から $80 まで
  • $80 から $100 まで

私は結合のさまざまなクエリをたくさん試しましたが、このようなネストされたクエリも試しました。

$qry = "SELECT * FROM `tours` WHERE status='1' ";
$qry1 = mysqli_query($con,$qry);
while($data = mysqli_fetch_array($qry1)){

$qfilter = "SELECT * FROM `tour_dates` WHERE tour_id='".$data['id']."' AND (`price` BETWEEN 10 AND 50) ORDER BY price DESC ";
    $qfilter1 = mysqli_query($con,$qfilter);
    $tour_ob = mysqli_fetch_object($qfilter1);
    $num = mysqli_num_rows($qfilter1);
    if($num>0){ 
     ------
      }
    }

解決策を教えてください。ありがとうございました。

4

1 に答える 1

0

結合を使用して、両方のテーブルからデータを取得できます。

select t1.id,t1.tour_title,t2.price  
from tours t1 inner join tour_dates t2 on t1.id=t2.tour_id 
where t1.id=tour_id and (t2.price between 10 and 50) order by t2.price desc

動的な where 条件を処理するストアド プロシージャを作成できます。

CREATE PROCEDURE `procedure_name` (in in_condition varchar(1000))
BEGIN
      set @query='select t1.id,t1.tour_title,t2.price  
                  from tours t1 inner join tour_dates t2 on t1.id=t2.tour_id 
                  where t1.id=tour_id ';

       set @query=concat(@query,' and ',in_condition,' order by t2.price desc');

        prepare stmt from @query;
        execute stmt;
        deallocate prepare stmt;
END
于 2015-07-24T15:31:26.483 に答える