使用内部联接sql查询从php

时间:2016-11-07 08:05:17

标签: php mysql date search inner-join

我是PHP和SQL的新手;我正在编写一个SQL查询来显示具有以下逻辑的记录:

  1. sql表中有9个单元格。我想使用3个参数的组合搜索记录。这是在2个日期之间搜索,搜索位置和搜索属性类别类型。

  2. 搜索条件如下所示:

  3.   

    日期自:_________(日期选择器) - 截止日期:______________(日期选择器)

         

    销售代理:下拉(dehi,mumbai,.....,)

         

    手机:__________(文字)

    结果要求组合:

    一个。全部3个组合True - (用户填写日期,销售代理,移动。)

    湾这两种组合都是真的。 (用户只填充一个参数。)

    ℃。只有2个组合是真的。 (用户填写2个参数组合,即日期和移动(或)移动和销售代理(或)销售代理和日期)

    问题:我不能只做一个组合。

    这是我的SQL查询和页面语法:

    if(isset($_POST["submit"]))
    {
        $date11=$_POST["date1"];
        $date22=$_POST["date2"];
        $salesagent1=$_POST["salesagent"];
        $mobile1=$_POST["mobile"];
    
        $result = "select 
                     ordertable.order_date, 
                     ordertable.order_id,
                     customer.cust_name, 
                     customer.cust_mobile,
                     customer.cust_email,
                     customer.cust_city,
                     ordertable.quantity, 
                     ordertable.total,
                     orderstatus.order_sta,
                     salesagent.name
                   from customer inner join ordertable 
                     on customer.custid=ordertable.cust_id inner join salesagent 
                     on salesagent.said=ordertable.sales_id inner join orderstatus 
                     on orderstatus.id= (select order_statusid from orderhistory where order_id1= ordertable.order_id  order by date_added desc limit 1)                 
                   where (ordertable.order_date between '$date11' and '$date22') or (customer.cust_mobile='$mobile1') or (ordertable.sales_id='$salesagent1') 
                   order by ordertable.order_id desc";
    
        $records=mysqli_query($CON,$result);
    

1 个答案:

答案 0 :(得分:0)

null或您选择的任何内容设置为每个参数为空时:

$result = "select 
         ordertable.order_date, 
         ordertable.order_id,
         customer.cust_name, 
         customer.cust_mobile,
         customer.cust_email,
         customer.cust_city,
         ordertable.quantity, 
         ordertable.total,
         orderstatus.order_sta,
         salesagent.name
         from customer inner join ordertable 
         on customer.custid=ordertable.cust_id inner join salesagent 
         on salesagent.said=ordertable.sales_id inner join orderstatus 
         on orderstatus.id= (select order_statusid from orderhistory where order_id1= ordertable.order_id  order by date_added desc limit 1) 

        where ('$date11' IS NULL OR '$date22' IS NULL OR ordertable.order_date between '$date11' and '$date22') AND ('$mobile1' IS NULL OR customer.cust_mobile='$mobile1') AND ('$salesagent1' IS NULL OR ordertable.sales_id='$salesagent1') 
         order by ordertable.order_id desc";