MySQL 中 SELECT 查询中带有 LIKE 的 SWITCH
我有这个标签表,
CREATE TABLE IF NOT EXISTS `Tags` (
`id_tag` int(10) unsigned NOT NULL auto_increment,
`tag` varchar(255) default NULL,
PRIMARY KEY (`id_tag`),
UNIQUE KEY `tag` (`tag`),
KEY `id_tag` (`id_tag`),
KEY `tag_2` (`tag`),
KEY `tag_3` (`tag`),
KEY `tag_4` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2937 ;
INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(1816, '(class'),
(2642, 'class\r\n\r\nâ?¬35'),
(1906, 'class\r\nif'),
(1398, 'class'),
(2436, 'class)'),
(1973, 'class:\n1.'),
(2791, 'classes'),
(1325, 'New'),
(2185, 'pack'),
(1905, 'packed'),
(1389, 'WebClass');
我想获取标签与关键字 class
或 pack
或 new
匹配的所有记录,以及指示哪个字段的另一个字段这3个关键字实际上与标签字段匹配。
以下查询没有给出正确的结果 查询 1
select id_tag,
case tag
when tag LIKE "%class%" then "class"
when tag LIKE "%new%" then "new"
when tag LIKE "%pack%" then "pack"
end as matching_tag
from Tags
where tag LIKE "%class%" OR tag LIKE "%new%" OR tag LIKE "%pack%"
我必须在案例中使用类似的内容。否则完成匹配工作。以下查询有效:-
查询 2
select id_tag,
case tag
when "class" then "class"
when "new" then "new"
when "pack" then "pack"
end as matching_tag
from Tags
where tag = "class" OR tag = "new" OR tag = "pack"
查询 1 有什么问题。请帮忙。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mysql 支持两种大小写变体,查询 2 中使用的变体不太灵活,但仅支持单个变量的相等性。另一个版本在 case 之后不指定变量,然后条件不必只是相等:
请参阅 文档 了解更多详细信息
编辑:
这里有更多关于为什么你的查询 #1 返回它返回的内容的解释:
期望获得一个文字值用于
when ... then
之间的比较在上述情况下,表达式
tag LIKE "%class%"
、tag LIKE "%new%"
和tag LIKE "%pack%"
都是在实际案例比较之前进行评估。然而(!),发生的情况是它们变成 0 或 1,并且与标记的值相比,第一个 0 值将匹配任何 char(char 将被转换为 0) - 这与结果一致您的第一个查询。
这是一个显示相关表达式的逻辑值的查询:
这就是为什么你会得到意想不到的结果;沉默的 CAST 是这里的一个标准陷阱。
Mysql supports two variants of case, the one you use in query 2 is less flexible but supports only equality on a single variable. The other version specifies no variable after case and then conditions need not be only equality:
See documentation for further details
EDIT:
Here's a bit more explanation on why your query #1 returned what it returned:
expects to get a literal value for comparison between
when ... then
In the above case the expressions
tag LIKE "%class%"
,tag LIKE "%new%"
andtag LIKE "%pack%"
are all evaluated before the actual case comparison.However (!), what happens is that they become either 0 or 1 and when compared to the value of tag it is the first value of 0 that will match any char (char will get cast to 0) - this is consistent with the results of your first query.
Here's a query that shows the logical values for the relevant expressions:
That's why you get unexpected results; the silent CAST is a standard pitfall here.
只是想提醒一下,关于 else 子句:
Just want remind, about else clause: