开发者

Question about SQL clause

开发者 https://www.devze.com 2023-01-02 08:12 出处:网络
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

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" ?

Regards

Javi


You could split your search terms and perform multiplie LIKE clauses. 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. 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%'


Would something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号