2012-11-06 72 views
0

下面是該查詢:添加檢查,以大型SQL查詢

SELECT tbl_product.id, tbl_productspecification.id AS specificationId, 
      tbl_product.ProductId, tbl_seller.CompanyName, tbl_product.ProductName, tbl_product.Description, mst_Categories.id AS 'Category', 
      tbl_productspecification.RetailPrice, tbl_productspecification.SalePrice, 
      tbl_product.image, tbl_productspecification.Discount, tbl_product.EndTime, tbl_product.Seller_Id 
      FROM tbl_product 
      LEFT OUTER JOIN tbl_seller ON tbl_seller.SelId = tbl_product.Seller_Id 
      LEFT OUTER JOIN mst_Categories ON (mst_Categories.id = tbl_product.Category OR mst_Categories.id = tbl_product.SubCategory) 
      LEFT OUTER JOIN tbl_productspecification ON tbl_productspecification.ProductId = tbl_product.ProductId 
      LEFT OUTER JOIN mst_image ON mst_image.Product = tbl_product.ProductId 
      LEFT OUTER JOIN tbl_dealinterest ON tbl_dealinterest.ProductId = tbl_product.ProductId 
      where tbl_product.Active='y' and tbl_product.StartTime <= '".date("Y-m-d H:i:s")."' and tbl_product.EndTime>'".date("Y-m-d")." 06:00:00' 
      ".$subquery." ".$groupby; 

tbl_dealinterest表有幾個領域:

[BuyerId] [ProductId] [Active] 

我需要過濾掉任何記錄都在tbl_dealinterest匹配[BuyerId][ProductId]那和[Active]不等於n

我已經嘗試了一些東西,但ñ所有的產品都列出了tbl_dealinterest。只有當某人選擇一個選項時纔會進入。

回答

1

這應該過濾掉tbl_dealinterest中的任何匹配記錄。假設tbl_dealinterest.BuyerId在存在記錄時永遠不會爲NULL。

如果不匹配,則tbl_dealinterest表的左連接將爲tbl_dealinterest表中的所有字段返回NULL值。 where子句中的「tbl_dealinterest.BuyerId IS NULL」將過濾出匹配項。

SELECT tbl_product.id, tbl_productspecification.id AS specificationId, 
      tbl_product.ProductId, tbl_seller.CompanyName, tbl_product.ProductName, tbl_product.Description, mst_Categories.id AS 'Category', 
      tbl_productspecification.RetailPrice, tbl_productspecification.SalePrice, 
      tbl_product.image, tbl_productspecification.Discount, tbl_product.EndTime, tbl_product.Seller_Id 
      FROM tbl_product 
      LEFT OUTER JOIN tbl_seller ON tbl_seller.SelId = tbl_product.Seller_Id 
      LEFT OUTER JOIN mst_Categories ON (mst_Categories.id = tbl_product.Category OR mst_Categories.id = tbl_product.SubCategory) 
      LEFT OUTER JOIN tbl_productspecification ON tbl_productspecification.ProductId = tbl_product.ProductId 
      LEFT OUTER JOIN mst_image ON mst_image.Product = tbl_product.ProductId 
      LEFT OUTER JOIN tbl_dealinterest ON tbl_dealinterest.BuyerId = tbl_product.BuyerId AND tbl_dealinterest.ProductId = tbl_product.ProductId AND tbl_dealinterest.active <> 'n' 
      where tbl_product.Active='y' and tbl_product.StartTime <= '".date("Y-m-d H:i:s")."' and tbl_product.EndTime>'".date("Y-m-d")." 06:00:00' 
      AND tbl_dealinterest.BuyerId IS NULL 
      ".$subquery." ".$groupby;