我有两张 table ,“旅游”表和“旅游日期”表。
- 旅游表
- 身份证
- tour_title
这是查看表格的链接 tour table !
- tour_dates 表
- ID
- tour_id
- 价格
这是查看表格的链接 tour_date table !
现在我正在使用价格范围过滤器,例如
- 从 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){
------
}
}
请提供任何解决方案。 谢谢。
最佳答案
您可以使用联接从两个表中获取数据。
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
关于php - 使用 Range 从两个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31614342/