【问题标题】:Displaying records using inner join sql query from more than 2 combinations parameters in php使用php中超过2个组合参数的内连接sql查询显示记录
【发布时间】:2026-02-07 09:10:01
【问题描述】:

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

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

  2. 搜索条件如下所示:

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

销售代理:下拉列表(dehi、mumbai、.....、)

手机:__________(文字)

需要结果组合:

一个。全部 3 个组合 True -(用户填写日期、销售代理、手机。)

b.任何一个组合都是 True。 (用户只填写其中一个参数。)

c。只有 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);

【问题讨论】:

标签: php mysql date search inner-join


【解决方案1】:

设置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";

【讨论】: