Max
Max

Reputation: 860

Where-clause with left joined tables

I have two tables: products and products_actions.

When the user clicks away a product, I want this action to be stored in products_actions. The field has_removed will be 1 then.

I would like to present to the user only those products that he has not clicked away.

Currently I have the following entries in my tables:

Table "products":

id: 1
name: Product 1

id: 2
name: Product 2

id: 3 
name: Product 3

Table "products_actions":

id: 1 
id_product: 2
has_removed: 1

If the user has not removed a product from his page so far, there will be no corresponding entry in the products_actions table.

So my query is:

$qb->select('p')
            ->leftJoin(
                'ProductsActions',
                'pa',
                'WITH',
                'pa.idProduct = p.id'
            ) 
            ->where('pa.hasRemoved != 1');

How do I achieve that my query aboves delivers "Product 1" and "Product 3" as entries?

Upvotes: 0

Views: 40

Answers (1)

Md. Mahmud Hasan
Md. Mahmud Hasan

Reputation: 1063

I am not entirely clear what you need. I guess you are talking probably like this :

select * from products where id not in (select id_product from products_actions where hasRemoved = 1)

Upvotes: 2

Related Questions