我在理解为什么我的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'
我的结果显示确定,我的规则显示Brand1,2和3.当我添加上面的行时,我希望看到相同的结果,但没有品牌3.品牌3没有显示哪个是伟大的,但它似乎现在它列出了所有品牌2,而不仅仅是前一个订单状态陈述为真的行。
我无法理解为什么我的结果不仅仅是返回我期望的11个结果(它返回425)几乎是Brand 2的大部分结果。
答案 0 :(得分:1)
你在OR子句周围缺少括号......它应该是:
(orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2')
如果你没有放入parens,那么约束会使用运算符优先级关联,这可能不是你想要的!
答案 1 :(得分:0)
试试这个:
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')
答案 2 :(得分:0)
试试这个
....AND orderheader.status != '1'
AND ( orderitems.webbrandcode = 'brand1' || orderitems.webbrandcode = 'brand2' )
AND orderheader.billingcustomeremailaddress != 'email1@email.co.uk'...