Find a Customer that buys only specific products & did not buy any other products in codeigniter query

60 Views Asked by At
 customers table     
| id | name | email           |
| 1  | richa| [email protected] |``
| 2  | sam  | [email protected]   |
| 3  | hella| [email protected] |

orders table
| id | customer_id |      
| 101   |    1     |
| 102   |    2     |
| 103   |    3     |

order_product table
| id | order_id | product_id |    
|    |    101   |   1        | 
|    |    102   |   1        |
|    |    103   |   2        |

 products table     
 | id   | product_name |     
 | 1    | Mobile       |      
 | 2    | AC           |      
 | 3    | Fridge       | 

These are tables, I want to display customers who buy only one type of products(can be multiple) but did not buy any other products.

$resultArr = $this->db->table('customers')``
            ->join('orders', 'orders.customer_id = customers.id')
            ->join('order_products', 'order_products.order_id = orders.id')
            ->select('customers.id, customers.name, customers.email');
            ->groupBy('customers.id');
            ->where('order_products.product_id', > 0);
        return $resultArr;

I added this query but i did not get required data. can anyone give any idea or explain me if i did any mistake in it let me know. Thanks in advance.

0

There are 0 best solutions below