在MySQL中WHERE子句中的情况 [英] CASE in WHERE CLAUSE in MYSQL

查看:128
本文介绍了在MySQL中WHERE子句中的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单,正如标题所言,但这是一种逻辑. 这是我的代码

The question is as simple as the title says,But here is one logic. Here is my code

CREATE TABLE `inf_brand_images` (
`id` bigint(99) NOT NULL AUTO_INCREMENT,
`brand` varchar(255) NOT NULL,
`thumb` text NOT NULL,
`is_active` int(2) NOT NULL DEFAULT '1',
`cmp_brand` varchar(1024) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6458 DEFAULT CHARSET=latin1

此表中的数据

ID | brand  | thumb  |is_active| cmp_brand
1  | NIKE   | a.png  | 1       | 
2  | DUNHILL| b.png  | 1       |
3  | NIKE   | c.png  | 1       | 123_NIKE
4  | NIKE   | d.png  | 1       | 789_NIKE

在我的情况下,cmp_brand的前缀是一些ID,例如123_和789_. 现在如果我搜索NIKE,那么我有两个参数,一个是NIKE,另一个是id_NIKE.其中id可能是123或456或任何其他.所以对于NIKE搜索,我有两个参数,一个是brand = NIKE,另一个是cmp_brand = 123_NIKE(我可以自动将ID与品牌名称关联起来)

cmp_brand is prefixed with some their ids like 123_ and 789_ in my case. Now if i search for NIKE, so I have two parameters,one is NIKE and other is id_NIKE.where id may be 123 or 456 or any other.So for NIKE search i have two parameters,one is brand=NIKE and other is cmp_brand =123_NIKE (i cancatenate id with brand name automatically)

我想要的是

IF cmp_brand is '' then compare with brand ELSE compare brand AND cmp_brand.

这是我尝试过的,都无法正常工作

Here is what i tried,both not working

SELECT thumb 
FROM inf_brand_images 
where is_active=1  AND 
CASE WHEN cmp_brand = '' THEN brand='NIKE' 
ELSE cmp_brand='123_NIKE' END

 SELECT thumb 
 FROM inf_brand_images 
 where is_active=1 AND 
((cmp_brand = '' AND brand='NIKE') OR (cmp_brand='123_NIKE'))

推荐答案

使用逻辑AND/OR分组而不是CASE分组,您第二次尝试位于正确的轨道上,但是如果您想更喜欢将匹配cmp_brand的行与空cmp_brand的行匹配,并且仅返回一个结果,对ORDER BY进行结构化以首先对非空的cmp_brand进行排序,并将总结果限制为1./p>

You are on the right track with your second attempt, using logical AND/OR groupings instead of a CASE, but if you want to prefer the row matching cmp_brand over rows with an empty cmp_brand and expect only one result back, structure your ORDER BY to sort the non-empty cmp_brand first, and limit the overall result to 1.

SELECT thumb 
FROM inf_brand_images 
WHERE
  is_active=1 AND 
  ((cmp_brand = '' AND brand='NIKE') OR (cmp_brand='123_NIKE'))
/* non-empty cmp_brand will sort first */
ORDER BY cmp_brand <> '' DESC
/* and the end result is limited only to the first sorted row
   which will be the cmp_brand if matched, or the brand otherwise */
LIMIT 1

http://sqlfiddle.com/#!2/d176b/2

之所以有效,是因为表达式cmp_brand <> ''的计算结果为布尔值true/false,MySQL将其解释为1/0.对这些值进行降序排序将迫使非空值对拳头排序(0之前为1).

This works because the expression cmp_brand <> '' evaluates to the boolean true/false, which MySQL interprets as 1/0. A descending sort on those values forces the non-empty ones to sort fist (1 before 0).

由于您确实有可能返回多于一行,因此您不能依赖ORDER BY.相反,您可以对同一张表执行LEFT JOIN.一方面,匹配cmp_brand = '',另一方面,匹配cmp_brand = '123_NIKE'.重要的是,从联接的两边返回thumb列.

Since you do have the possibility of more than one row returned, you cannot rely on the ORDER BY. Instead, you can perform a LEFT JOIN against the same table. On one side, match cmp_brand = '' and on the other side match cmp_brand = '123_NIKE'. Importantly, return the thumb column from both sides of the join.

将其包装在FROM子句中的子查询中,然后在顶层使用SELECT CASE优先使用cmp_brand(如果为非空).

Wrap that in a subquery in the FROM clause, then at the top level you can use a SELECT CASE to prefer the cmp_brand if nonempty.

SELECT DISTINCT
  CASE WHEN cbcb IS NOT NULL THEN cbthumb ELSE bthumb END AS thumb
FROM (
  /* Return thumbs from both sides of the join */
  SELECT 
    b.thumb AS bthumb,
    b.cmp_brand AS bcb,
    cb.thumb AS cbthumb,
    cb.cmp_brand AS cbcb
  FROM
    inf_brand_images b
    /* join the table against itself with the matching cmp_brand in the join condition */
    LEFT JOIN inf_brand_images cb
      ON b.brand = cb.brand
      AND cb.cmp_brand = '123_NIKE'
  WHERE 
    /* The WHERE clause looks for empty cmp_brand on the left side of the join */
    b.brand = 'NIKE' AND b.cmp_brand = ''
) thumbs

  • 以下是123_NIKE匹配的示例: http://sqlfiddle.com/#!2/dfe228/31
  • 还有一个示例,其中124_NIKE不匹配: http://sqlfiddle.com/#! 2/dfe228/32
    • Here is an example where 123_NIKE matches: http://sqlfiddle.com/#!2/dfe228/31
    • And an example where 124_NIKE does not match: http://sqlfiddle.com/#!2/dfe228/32
    • 这篇关于在MySQL中WHERE子句中的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆