Mysql 使用 and 运算符查询无效

Mysql query not work using and oprator

使用以下 mysql 查询时:

select * from gc_product_attribute where (attribute_id = 30 AND text like '62') and (attribute_id = 35 AND text like '71')

值存在于 table 但没有得到 return
table 下面

attribute_id | text
 30          |  62
 35          |  71

我猜你正在找这个

(attribute_id = 30 AND 文本如“62”) OR (attribute_id = 35 AND 文本如“71”)

肯定没有 attribute_id 30 和 35 的行。

请尝试:

select * 
from gc_product_attribute 
where attribute_id=30 
AND text like '%62%' 
OR (attribute_id = 35 AND text like '%71%') 

您想 return 包含 (attribute_id = 30 AND 文本如“62”) 的项目 包含 (attribute_id = 35 AND 文本如'71').

这不同于检索具有(attribute_id = 30 AND 文本如“62”)和具有(attribute_id = 35 AND 的项目文本如“71”)

可以用两种方式表达:

  • 检索满足 X 的项以及满足 Y 的项:UNION

    select * from gc_product_attribute where (attribute_id = 30 AND text like '62') UNION select * from gc_product_attribute where (attribute_id = 35 AND text like '71')

  • 检索满足 X 或 Y 的项目:OR

    select * from gc_product_attribute where (attribute_id = 30 AND text like '62') OR (attribute_id = 35 AND text like '71')