MySQL Forums
Forum List  »  General

Re: Please help with complex where
Posted by: Peter Brawley
Date: December 04, 2018 09:58AM

Basically the problem is the schema. Order completion looks like a property of an order, not an order detail. Your query will be easier with that.

And BTW BigInts for PKs!? You expect to have 18 billion billion customers, and that many orders and order items? Int unsigned would be better and faster for all those tables.

And an issue with in In(Select...), in many MySQL versions such semi-joins are slower.

So, on your present schema, customers with complete orders would be ...

select ...
from customers c
join orders o using(customerid)
join orderdetails d1 on o.orderid=d1.orderid and d1.assignment=1
join orderdetails d2 on o.orderid=d2.orderid and d2.assignment=2 ;

... and finding the most recent order for each customer is probably best done with an orders subquery ...

select c.customerid, x.latest
from customer c
join (
  select o.orderid, max(o.orderdate) as latest
  from orders o
  join orderdetails d1 on o.orderid=d1.orderid and d1.assignment=1
  join orderdetails d2 on o.orderid=d2.orderid and d2.assignment=2 ;
  group by o.orderid
) x using(customerid);

One other note, databases have a habit of migrating to different platforms over time, Linux is case-sensitive, so capitalisation in table & column names is not a terrific idea.

Options: ReplyQuote


Subject
Written By
Posted
December 04, 2018 04:22AM
Re: Please help with complex where
December 04, 2018 09:58AM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.