簡體   English   中英

使用PHP中2個以上組合參數的內部連接SQL查詢顯示記錄

[英]Displaying records using inner join sql query from more than 2 combinations parameters in php

我是PHP和SQL的新手。 我正在編寫一個SQL查詢以顯示具有以下邏輯的記錄:

  1. sql表中有9個單元格。 我想使用3個參數的組合來搜索記錄。 即在2個日期之間進行搜索,在位置中搜索並在屬性類別類型中搜索。

  2. 搜索條件如下所示:

起始日期:_________(日期選擇器)-日期耕種:______________(日期選擇器)

銷售代理:下拉菜單(德里,孟買,.....,)

手機:__________(文本)

需要結果組合:

一種。 所有3種組合的True-(用戶填寫日期,銷售代理商,移動電話。)

b。 兩種組合均為真。 (用戶僅填寫一個參數。)

C。 只有2個組合為True。 (用戶填寫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);

當每個參數為空時,將其設置為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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM