梦想之翼
发布于

返回所有单词而忽略一个单词的 Query

我有这个表格和下一条记录:

  
CREATE TABLE `t_vias` (
`id` int(11) NOT NULL,
`cpro` varchar(2) NOT NULL,
`cmum` varchar(3) NOT NULL,
`cpos` varchar(5) NOT NULL,
`mun` varchar(75) NOT NULL,
`via` varchar(100) NOT NULL);
 
     
INSERT INTO t_vias VALUES
("6021","28","079","28016","MADRID","GOYA, FRANCISCO DE"),
("6022","28","079","28016","MADRID","FRANCISCO GOYA"),
("6896","28","079","28001","MADRID","GOYA, DE"),
("6897","28","079","28001","MADRID","GOYA (DE)"),
("6900","28","079","28001","MADRID","CALLE GOYA"),
("6901","28","079","28001","MADRID","CALLE DE GOYA");

我从 php 变量收到值 “CALLE DE GOYA”,并在查询中包含变量值:

  
SELECT * FROM `t_vias` WHERE via ='CALLE DE GOYA'; 

结果仅显示一条记录:

  
id  cpro    cmum    cpos    mun     via     
6901    28  079     28001   MADRID  CALLE DE GOYA

既然在这里,就可以了。但是,如果我想显示所有注册“GOYA”字(删除 CALLE、DE 等):

  
SELECT * FROM `t_vias` WHERE via ='%GOYA%'; 
 id     cpro    cmum    cpos    mun     via     
6021    28  079     28016   MADRID  GOYA, FRANCISCO DE
6022    28  079     28016   MADRID  FRANCISCO GOYA
6896    28  079     28001   MADRID  GOYA, DE
6897    28  079     28001   MADRID  GOYA (DE)
6900    28  079     28001   MADRID  CALLE GOYA
6901    28  079     28001   MADRID  CALLE DE GOYA

返回所有带有 GOYA 的条目。

问题是如果变量的值是这样的:'DE GOYA'

然后查询:

  
SELECT * FROM `t_vias` WHERE via LIKE '%DE GOYA%'; 

id  cpro    cmum    cpos    mun     via     
6901    28  079     28001   MADRID  CALLE DE GOYA

仅返回一条记录。如何搜索没有 DE 的变量值,如果它包含 DE (DE)?

浏览 (38)
点赞
收藏
1条评论
Klustron小助手
建议去正则表达式 SELECT * FROM `t_vias` WHERE via regexp "GOYA";
点赞
评论