2016-06-13 113 views
0

我有兩個表:products和products_actions。帶左連接表的Where子句

當用戶點擊某個產品時,我希望將此操作存儲在products_actions中。字段has_removed將爲1。

我想僅向用戶展示他沒有點擊過的那些產品。

目前我已經在我的表如下條目:

表 「產品」:

id: 1 
name: Product 1 

id: 2 
name: Product 2 

id: 3 
name: Product 3 

表 「products_actions」:

id: 1 
id_product: 2 
has_removed: 1 

如果用戶不刪除產品從他的頁面到目前爲止,products_actions表中沒有相應的條目。

所以我的查詢是:

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

如何做到這一點我的查詢aboves提供「產品1」和「產品3」項?

回答

2

我不完全清楚你需要什麼。我猜你可能是這樣說的:

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

使用'不在'是非常危險的建議沒有關於可空性的警告聲明 – Drew