代码之家  ›  专栏  ›  技术社区  ›  androniennn

检查连接查询中两列中的2个值

  •  0
  • androniennn  · 技术社区  · 7 年前

    prd_品牌

    • 名称

    • rowid公司

    我将这两个表连接在一起,如下所示:

    SELECT main_table.* 
    FROM prd_brand AS main_table 
    INNER JOIN catalog_product_entity_int 
    ON main_table.brand_id=catalog_product_entity_int.value 
    group by brand_id 
    order by name asc
    

    我现在想做的是办理登机手续 catalog_product_entity_int attribute_id 97 有一个 value 1.

    prd_品牌

    brand_id    |   name
    26          |   Nivea
    44          |   Ducray
    

    attribute_id    |   rowid   |   value 
    198             |   174     |   26 
    97              |   174     |   1
    788             |   174     |   4
    198             |   210     |   44
    97              |   210     |   0
    

    catalog\u product\u entity\u int 表,its rowid 是174,这个 rowid公司 中的值为1 97=>我们接受了。

    Ducray(ID 44)存在于 表,its 中的值为0 属性id 97=>我们不接受。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Blank    7 年前

    在这里,我用 group_concat find_in_set :

    select distinct a.*
    from prd_brand a
    join (
        select
            group_concat(attribute_id order by attribute_id) attrs, 
            group_concat(`value` order by attribute_id) vals
        from catalog_product_entity_int
        group by `rowid`
    ) b
    on find_in_set(a.brand_id, b.vals)
    and find_in_set('97', b.attrs) > 0
    and find_in_set('1', b.vals);
    

    demo 在这里

    join 带子查询的解决方案:

    select distinct a.*
    from prd_brand a
    join catalog_product_entity_int b1
    on a.brand_id = b1.`value`
    and exists (
        select 1
        from catalog_product_entity_int b2
        where b1.rowid = b2.rowid
        and b2.`value` = 1
        and b2.`attribute_id` = 97
    )
    

    demo

        2
  •  1
  •   Oto Shavadze    7 年前

    一种可能的方式

    select prd_brand.* from prd_brand
    inner join
    (
        select distinct value from catalog_product_entity_int
        where 
        rowid in (select rowid from catalog_product_entity_int where attribute_id = 97 and value = 1)
    )t
    on prd_brand.brand_id = t.value