简体   繁体   English

MYSQL中WHERE子句的情况

[英]CASE in WHERE CLAUSE in MYSQL

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

Here is data in this table 这是此表中的数据

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 is prefixed with some their ids like 123_ and 789_ in my case. 在我的情况下,cmp_brand的前缀是一些ID,例如123_和789_。 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) 现在如果我搜索NIKE,那么我有两个参数,一个是NIKE,另一个是id_NIKE。其中id可能是123或456或任何其他。所以对于NIKE搜索,我有两个参数,一个是brand = NIKE,另一个是cmp_brand = 123_NIKE(我可以自动将ID与品牌名称关联起来)

What i want is 我想要的是

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'))

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. 你是在正确的轨道与你的第二次尝试时,使用逻辑AND/OR分组,而不是一个CASE ,但如果你想喜欢的行匹配cmp_brand了行与空cmp_brand和期望结果只有一个回来,结构中的ORDER BY来首先对非空的cmp_brand排序,并将总结果限制为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 http://sqlfiddle.com/#!2/d176b/2

This works because the expression cmp_brand <> '' evaluates to the boolean true/false , which MySQL interprets as 1/0 . 之所以有效,是因为表达式cmp_brand <> ''评估为布尔值true/false ,MySQL将其解释为1/0 A descending sort on those values forces the non-empty ones to sort fist (1 before 0). 对这些值进行降序排序将迫使非空值对拳头排序(0之前为1)。

Update after comments: 评论后更新:

Since you do have the possibility of more than one row returned, you cannot rely on the ORDER BY . 由于您确实有可能返回多于一行,因此您不能依赖ORDER BY Instead, you can perform a LEFT JOIN against the same table. 相反,您可以对同一张表执行LEFT JOIN On one side, match cmp_brand = '' and on the other side match cmp_brand = '123_NIKE' . 一方面,匹配cmp_brand = '' ,另一方面,匹配cmp_brand = '123_NIKE' Importantly, return the thumb column from both sides of the join. 重要的是,从联接的两侧返回thumb列。

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. 将其包装在FROM子句中的子查询中,然后在顶层使用SELECT CASE cmp_brand使用cmp_brand如果cmp_brand空)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM