【问题标题】:MYSQL Union and LIKEMYSQL联合和LIKE
【发布时间】:2012-02-27 22:16:06
【问题描述】:

大家好,我在将 LIKE 添加到我的 UNION 时遇到困难,它似乎要么返回任何东西,要么只返回 UNION 中指定的项目,具体取决于我喜欢的位置,例如

完整查询

SELECT  fruits.key, 
        region, location, report_date, 
        inspection_type AS type, 
        customer AS customer_name, 
        customer_number, shipper, po

    FROM reports

JOIN (      ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `berries`       )
    UNION   ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `melons`        )
    UNION   ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `citrus`        )
    UNION   ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `table_grapes`  )
    UNION   ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `tree_fruit`    )
    UNION   ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `lot`           )
    ) fruits 

ON inspection_number = fruits.report_key

WHERE fruits.status = '0' OR fruits.status = '1'

ORDER BY report_date DESC

返回

key     region              location        report_date     type    customer_name   customer_number     shipper     po
17      Great White North   South Holywood  2012-10-21      citrus  Name            206-420-9564        Yao Ming    4215
16      Great White North   Boulder, CO     2012-10-21      citrus  Name            206-420-9564        Hanjin      33215
21      nw                                  1969-12-31      citrus  2               321                 sdfg        sdfgs
20      nw                  sdfgsdfg        1969-12-31      citrus  2               321                 sdfg        sdfg

尝试一下

WHERE fruits.status = '0' OR fruits.status = '1' AND fruits.location LIKE '%Boulder%'

返回完全相同的内容,而不仅仅是位置中带有 Boulder 的行。

另一方面,这将返回该行,但不会返回来自reports 的其余信息

`WHERE fruits.location LIKE '%Bolder%' AND fruits.status = '0' OR fruits.status = '1'

key     region              location        report_date     type    customer_name   customer_number     shipper     po
16      Great White North   Boulder, CO     2012-10-21      citrus  Jd Daniel       206-420-9564        Hanjin      33215

这样做的正确方法是什么?我似乎得到了一个的一半,或者一个都没有?

【问题讨论】:

    标签: mysql select join union sql-like


    【解决方案1】:

    您的问题是 AND 比 OR 更难绑定,这意味着您的 where 子句最终为

    WHERE fruits.status = '0' OR 
         (fruits.status = '1' AND fruits.location LIKE '%Boulder%')
    

    你的意思大概是

    WHERE (fruits.status = '0' OR fruits.status = '1') 
       AND fruits.location LIKE '%Boulder%'
    

    这意味着您必须在 OR 表达式周围添加括号。

    【讨论】:

      【解决方案2】:

      不要在没有加括号的情况下混合 ors & ands。这可能就是你想要的:

      WHERE (fruits.status = '0' OR fruits.status = '1') AND fruits.location LIKE '%Boulder%'
      

      【讨论】:

        【解决方案3】:

        我希望我明白你的意思。如果是这样,您需要装订:

        WHERE fruits.status = '0' OR (fruits.status = '1' AND fruits.location LIKE '%Boulder%')
        

        WHERE (fruits.status = '0' OR fruits.status = '1') AND fruits.location LIKE '%Boulder%'
        

        希望有帮助:)

        【讨论】:

          猜你喜欢
          • 2018-05-22
          • 2020-05-28
          • 1970-01-01
          • 2011-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多