title: Filter customers based on products they bought
body: <p>I have 3 tables: </p>
  
  <ul>
  <li>customer (PK: customer_id)</li>
  <li>customer_order (PK: order_id, FK: customer_id, product_id)</li>
  <li>product (PK: product_id)</li>
  </ul>
  
  <p>A row of customer_order contains a customer and a product that customer bought in that order. </p>
  
  <p>Now for a management system, I have to input a list of products and find a list of customers who have bought all these products (inclusive). By inclusive I mean, all those customers that are filtered must have bought all the products.</p>
  
  <p>Things to keep in mind: </p>
  
  <ul>
  <li>Each customer returned may have bought more products. (input product list may be a subset)</li>
  <li>Number of products sent as input can vary</li>
  <li>I am trying to write in JPQL.</li>
  </ul>
  
  <p>In mysql I have this working with something like this in the customer_order table: </p>
  
  <pre><code>select customer_id from  (SELECT count(distinct (product_id)) as prodcount, customer_id FROM customer_order where product_id = :prod_id1 or product_id = :prod_id2 group by customer_id) as temp where temp.prodcount= 2;
  </code></pre>
  
  <p>Here number of product_id is variable: prod_id1, prod_id2, prod_id3, and so on. In the <code>temp.prodcount= 2</code>, '2' is the number products as input. </p>
  
  <p>I am counting the distinct number of products bought by a customer from the customer_order table after filtering the orders by input product list (where clause). And then comparing the count with the number of products input. If they are equal it means this is the customer who has bought all the products that are input </p>
  
  <p>I am not sure this is the correct way of doing this as it seems more like a hack. Moreover, I need to write in JPQL. So a JPQL solution would be helpful. </p>