php - 使用 Range 从两个表中获取数据

标签 php mysql mysqli mysqli-multi-query subquery

我有两张 table ,“旅游”表和“旅游日期”表。

  1. 旅游表
    • 身份证
    • 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/

    相关文章:

    javascript - jQuery 向 AJAX 调用或 CSS 解决方案添加另一个功能?

    接近 ASP.NET MVC 实现的 PHP 框架

    php - 如何在主主键的基础上设置重复值的主键?

    php - 从隐藏的表单值中获取来自 silenium php-webdriver 的文本

    php - 使用数据库中的 session 数据进行缩放

    php - 为什么要使用外键约束?

    mysql - Rails 部署 - 数据库未上传

    php - Laravel - 多种关系 Eloquent

    php - 我有电子邮件和密码 session 变量,但无法获取其他 session 变量(PHP、MySQL)

    PHPUnit 忽略 Apache 环境变量