我有两张桌子,巡回赛表和旅游日期表。
这是表格的链接tour table!
这是表格的链接tour_date table!
现在我正在使用价格区间的过滤器,如
我尝试过很多不同的连接查询,甚至尝试过像这样的嵌套查询。
$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){
------
}
}
请提供任何解决方案。 谢谢。
答案 0 :(得分: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
您可以编写存储过程来处理动态的条件。
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