简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM