PHP查询WHERE, AND &OR语句发出


PHP Query using WHERE, AND & OR Statement Issue

我无法理解为什么我的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。Brand 3没有显示哪个是好的,但现在它似乎列出了所有Brand 2,而不仅仅是之前的订单状态语句为真的行。

我不明白为什么我的结果不只是返回我期望的11个结果(它返回425)几乎大多数品牌2的结果

您缺少OR子句周围的括号…它应该是:

(orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2')

当你不放置父级时,约束会使用操作符优先级关联,这可能不是你想要的!

试试这个:

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(使用括号组成复数)表达式).

SELECT * FROM Persons WHERE
LastName='Svendson'
AND (FirstName='Tove' OR FirstName='Ola')

-> http://www.w3schools.com/sql/sql_and_or.asp

try this

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