[英]Displaying records using inner join sql query from more than 2 combinations parameters in php
我是PHP和SQL的新手。 我正在编写一个SQL查询以显示具有以下逻辑的记录:
sql表中有9个单元格。 我想使用3个参数的组合来搜索记录。 即在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.