【问题标题】:Mysql query not work using and opratorMysql查询不能使用and运算符
【发布时间】:2015-09-09 13:16:08
【问题描述】:

使用以下mysql查询时:

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

值存在于表中,但没有返回值
下表

attribute_id | text
 30          |  62
 35          |  71

【问题讨论】:

    标签: and-operator


    【解决方案1】:

    您想要返回具有 (attribute_id = 30 AND text like '62')的项目以及具有 (attribute_id = 35 AND text like '71')的项目

    这不同于检索具有 (attribute_id = 30 AND text like '62') 并且具有 (attribute_id = 35 AND text like '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')

    【讨论】:

      【解决方案2】:

      我猜你正在寻找这个

      (attribute_id = 30 AND text like '62') OR (attribute_id = 35 AND text like '71')

      肯定没有attribute_id 为30 和35 的行。

      【讨论】:

      • attribute_id 30 和 35 以及 text 62 和 71 分别存在。
      • 是的,但不是一次针对任何给定的一行,这就是您的查询未返回任何行的原因。使用我上面建议的 OR,如果您有两行如下所示,它应该返回两行 1.attribute_id 30 和 text 62 的组合 2.attribute_id 35 和 text 71 的组合
      【解决方案3】:

      请尝试:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-22
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        • 1970-01-01
        • 2019-07-10
        相关资源
        最近更新 更多