I’m creating a php/mysql app and I’m trying to run a query based on a user supplied date, then using those results return the query if another date does not exist or match.
I have multiple orders that I want to query based on a few things, like so:
- The user supplies a date.
MySQL uses supplied date and returns the rows only if there are NO “Orders” that have a more present date associated to that Customer_ID. So I only want a value if there are NO newer orders based on the user supplied date.
Here’s what I’ve been playing with:
SELECT
o.Customer_ID, o.ShippingCompanyName, cg.Category, cd.Category_ID, o.Order_ID,
SUM(o.CustomerOrderTotal) as TOTAL,
COUNT(o.Order_ID) as ORDERS,
MAX(o.OrderPlaceServerTime) as LASTORDER
FROM Orders o
LEFT JOIN CustomerDetails cd ON o.Customer_ID = cd.Customer_ID
LEFT JOIN _CustomerCategory cg ON cg.Category_ID = cd.Category_ID
WHERE (
o.OrderPlaceServerTime <= '".$BEFORE."'
AND o.OrderPlaceServerTime NOT BETWEEN '".$BEFORE."' AND NOW()
)
AND o.IsVOID = 0
AND o.IsPENDING = 0
GROUP BY o.Customer_ID
ORDER BY TOTAL DESC
I’m not getting the results I want. I gives me Customer Orders that also have newer orders than the user supplied date.
Also the “Dates” are like ‘2010-10-10 10:05:55’ so with data & time.
I’m a bit lost so I’m hoping someone can help me here or point me in the right direction.
Thanks.
You are mixing two things in the same query.
Each time the WHERE expressions run they check a SINGLE row! They do not check other rows. So when you check the dates you are basically saying WHERE i is less then 5 and i is not greater than 5. Which is pointless – you already said less then 5 in the first expression.
Change the WHERE to: