0

I have Two tables, Tour table and Tour Dates table.

  1. tours Table
    • Id
    • tour_title

Here's a link of table to view tour table!

  1. tour_dates Table
    • id
    • tour_id
    • price

Here's a link of table to view tour_date table!

Now I am using filter of price range like

  • From $10 to $50
  • From $50 to $80
  • From $80 to $100

I have tried lots of different queries of join and even tried for nested query like this one.

$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){ 
     ------
      }
    }

Please provide any solution. thank you.

Adas
  • 309
  • 1
  • 18

1 Answers1

0

You can use joins to get data from both tables.

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

You can write stored procedure which will handle your dynamic where conditions.

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
Abhishek Ginani
  • 4,511
  • 4
  • 23
  • 35
  • Hi, thank you for the reply, your query is good, but i am using check box for all three, i mean it could be from **10-50** and **80-100** both. Or all of three options. then can you please change your query according to that, as i have used two **Between** using **OR**, but didn't work. – Adas Jul 24 '15 at 15:41
  • Well i again tried with your previous query and added additional Between using OR in the query. and it just worked. Thank you very much for your help, God bless you! – Adas Jul 24 '15 at 16:03
  • Welcome Adas. By the way if you want dynamic conditions you can try the procedure also. – Abhishek Ginani Jul 24 '15 at 16:05