检查连接查询中 2 列中的 2 个值
Check 2 values in 2 columns in a join query
我有两个 tables :
prd_brand
- brand_id
- 姓名
catalog_product_entity_int
- attribute_id
- 行号
- 价值
我加入了这两个 table,如下所示:
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
table 如果 attribute_id
97 有 value
1。如果它的值不是 1,则不要取它。
prd_brand
brand_id | name
26 | Nivea
44 | Ducray
catalog_product_entity_int
attribute_id | rowid | value
198 | 174 | 26
97 | 174 | 1
788 | 174 | 4
198 | 210 | 44
97 | 210 | 0
妮维雅(ID 26)存在于catalog_product_entity_int
table,它的rowid
是174,这个rowid
在attribute_id
中的值为1 97 => 我们接受它。
Ducray(ID 44)存在于catalog_product_entity_int
table,其rowid
为210,此rowid
在attribute_id
中值为0 97 => 我们不接受它。
这里我用group_concat
and 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。
一种可能的方式
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
我有两个 tables :
prd_brand
- brand_id
- 姓名
catalog_product_entity_int
- attribute_id
- 行号
- 价值
我加入了这两个 table,如下所示:
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
table 如果 attribute_id
97 有 value
1。如果它的值不是 1,则不要取它。
prd_brand
brand_id | name
26 | Nivea
44 | Ducray
catalog_product_entity_int
attribute_id | rowid | value
198 | 174 | 26
97 | 174 | 1
788 | 174 | 4
198 | 210 | 44
97 | 210 | 0
妮维雅(ID 26)存在于catalog_product_entity_int
table,它的rowid
是174,这个rowid
在attribute_id
中的值为1 97 => 我们接受它。
Ducray(ID 44)存在于catalog_product_entity_int
table,其rowid
为210,此rowid
在attribute_id
中值为0 97 => 我们不接受它。
这里我用group_concat
and 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。
一种可能的方式
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