简体   繁体   English

MySQL过滤结果基于另一个表

[英]Mysql filter result based off of another table

I have some tables and am wanting to remove the ones that exist in another table from the result. 我有一些表,并且想要从结果中删除另一个表中存在的表。

Here is the first call so you can see what the table looks like that I am working with: 这是第一个电话,因此您可以看到我正在使用的表的外观:

    SELECT a.id_product, b.id_product_attribute 
    FROM ps_product_lang a 
    LEFT JOIN ps_product_attribute b 
    ON (a.id_product = b.id_product)

Table: 表:

id_product | id_product_attribute
   25              null
   26              null
   27              192
   27              193
   27              194
   27              195

In my other table I have this: 在我的另一个表中,我有这个:

SELECT * FROM eds_combinable_products

id_product  |  id_product_attribute
   25                 null
   27                 194

I tried to filter my result by making this call, but have had no luck with the products that do not have a product attribute. 我试图通过进行此调用来过滤结果,但是对于没有产品属性的产品没有运气。 It will remove the ones with an attribute just fine. 它将删除具有适当属性的对象。

SELECT a.id_product, b.id_product_attribute 
FROM ps_product_lang a 
LEFT JOIN ps_product_attribute b 
ON (a.id_product = b.id_product)
WHERE NOT EXISTS(
      SELECT c.id_product, c.id_product_attribute 
      FROM eds_combinable_products as c
      WHERE a.id_product = c.id_product
                    AND
            b.id_product_attribute = c.id_product_attribute
      )

Or, perhaps more simply: 或者,也许更简单地说:

SELECT  a.id_product, 
        b.id_product_attribute 
FROM    ps_product_lang a LEFT JOIN ps_product_attribute b ON (a.id_product = b.id_product)
        LEFT JOIN eds_combinable_products as c on a.id_product = c.id_product
WHERE   c.id_product_attribute is not null

I figured it out by adding an IF statement to the NOT EXISTS call 我通过在NOT EXISTS调用中添加IF语句来弄清楚

SELECT a.id_product, b.id_product_attribute FROM ps_product_lang a 
LEFT JOIN ps_product_attribute b ON (a.id_product = b.id_product)
WHERE NOT EXISTS(SELECT c.id_product, c.id_product_attribute FROM eds_combinable_products as c WHERE      a.id_product = c.id_product AND IF(b.id_product_attribute,b.id_product_attribute,0) = c.id_product_attribute)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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