Php 使用WHERE和&;或声明问题

Php 使用WHERE和&;或声明问题,php,mysql,where-clause,Php,Mysql,Where Clause,我很难理解为什么我的OR语句不能产生我想要的结果 if ($result = $mysqli->query("SELECT orderheader.ordernumber As Order_Number , ccilog.orderid AS Order_ID , orderheader.billingcontactfirstname AS Customer_Name , orderheader.billingcontactlastname AS Customer_Surname , o

我很难理解为什么我的OR语句不能产生我想要的结果

if ($result = $mysqli->query("SELECT 
orderheader.ordernumber As Order_Number
, ccilog.orderid AS Order_ID
, orderheader.billingcontactfirstname AS Customer_Name
, orderheader.billingcontactlastname AS Customer_Surname
, orderheader.billingcustomeremailaddress AS Customer_Email
, orderheader.webbrandcode AS Brand_Code
, orderitems.productname AS Product_Name
, orderitems.qty AS Item_Qty
, orderheader.datecreated AS Order_Date 
FROM orderheader 
LEFT JOIN ccilog ON orderheader.id=ccilog.orderid
LEFT JOIN orderitems ON ccilog.orderid=orderitems.orderid 
WHERE orderitems.status = '0' 
AND orderheader.status != '1'
AND orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2' 
AND orderheader.billingcustomeremailaddress != 'email1@email.co.uk'
AND orderheader.billingcustomeremailaddress != 'email2@email.co.uk'
"))
如果我删除该行:

AND orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2' 
我的结果显示ok,我的规则显示Brand1、2和3。当我加入上面这一行时,我希望看到相同的结果,但没有品牌3。品牌3没有显示哪个是好的,但现在它似乎列出了品牌2的所有内容,而不仅仅是前面的订单状态陈述为真的行

我不明白为什么我的结果不只是返回我预期的11个结果(返回425个),几乎是品牌2的大部分结果。

试试这个:

if ($result = $mysqli->query("SELECT 
orderheader.ordernumber As Order_Number
, ccilog.orderid AS Order_ID
, orderheader.billingcontactfirstname AS Customer_Name
, orderheader.billingcontactlastname AS Customer_Surname
, orderheader.billingcustomeremailaddress AS Customer_Email
, orderheader.webbrandcode AS Brand_Code
, orderitems.productname AS Product_Name
, orderitems.qty AS Item_Qty
, orderheader.datecreated AS Order_Date 
FROM orderheader 
LEFT JOIN ccilog ON orderheader.id=ccilog.orderid
LEFT JOIN orderitems ON ccilog.orderid=orderitems.orderid 
WHERE orderitems.status = '0' 
AND orderheader.status != '1'
AND (orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2')
AND orderheader.billingcustomeremailaddress != 'email1@email.co.uk'
AND orderheader.billingcustomeremailaddress != 'email2@email.co.uk'
"))
见:

您还可以组合AND和OR(使用括号形成复数) 表达式)


->

OR子句周围缺少括号…应为:

(orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2')
如果不放置参数,则约束将使用运算符优先级关联,这可能不是您想要的

试试这个

....AND orderheader.status != '1'
AND ( orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2' )
AND orderheader.billingcustomeremailaddress != 'email1@email.co.uk'...
....AND orderheader.status != '1'
AND ( orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2' )
AND orderheader.billingcustomeremailaddress != 'email1@email.co.uk'...