简体   繁体   English

有关SQL子句的问题

[英]Question about SQL clause

I have a database with names like this: 我有一个名称如下的数据库:

Maria Garcia Garralon 
Jose Maria Perez Gutierrez

I have a filter also to search the names. 我也有一个过滤器来搜索名称。 Imagine the user write in the filter 想象一下用户写在过滤器中

maria garralon 玛丽亚·加拉隆

(the first and the last words) (第一个和最后一个字)

In that case, is there any SQL clause that finds the record "Maria Garcia Garralon" ? 在那种情况下,是否有任何SQL子句找到记录“ Maria Garcia Garralon”?

Regards 问候

Javi 哈维

You could split your search terms and perform multiplie LIKE clauses. 您可以拆分搜索词并执行多个LIKE子句。 Such as: 如:

WHERE
    Field LIKE '%maria%'
AND
    Field LIKE '%garralon%'

Alternatively, if you can be sure of the order of the search terms ans spaces between them then you could always do something like this. 另外,如果您可以确定搜索词的顺序以及它们之间的空格,则可以始终执行类似的操作。

WHERE
    Field LIKE REPLACE('maria garralon', ' ', '%')

Yes, you can use a hideous non-scalable query segment along the lines of: 是的,您可以按照以下方式使用可怕的不可扩展查询段:

where upper(name) like 'MARIA % GARRALON'

or you can do it the right way :-) 或者你可以用正确的方法做:-)

Introduce another column like first_and_last_name and use an insert/update trigger to populate it with the correct value maria garralon . 引入诸如first_and_last_name类的另一列,并使用插入/更新触发器为其填充正确的值maria garralon Then index that column and your queries will be the blindingly fast: 然后为该列建立索引,您的查询将非常快:

where first_and_last_name = 'maria garralon'

That moves the cost of calculation to the insert/update where it belongs, rather than the select where the cost is incurred unnecessarily. 这会将计算成本移动到它所属的插入/更新位置,而不是不必要地产生成本的选择位置。 The data only changes on insert/update so that's the only time you should need to incur that cost. 数据仅在插入/更新时发生更改,因此这是您唯一需要承担此费用的时间。

If your needs are more complicated than a simple first and last name, you can populate other tables in your triggers. 如果您的需求比简单的名字和姓氏更复杂,则可以在触发器中填充其他表。

I guess you'd need to compare twice: 我想您需要比较两次:

(pseudo syntax) WHERE name LIKE '%Maria%' AND name LIKE '%Garralon%' (伪语法)名称LIKE'%Maria%'和名称LIKE'%Garralon%'

Would something like: 会是这样的:

select * from <TABLE>
 where <FIELD> like '%'+REPLACE(<NAME>,' ',' % ')+'%'

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

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