简体   繁体   中英

Search 2 columns for parts of string in MYSQL

I would like to search inside 2 columns for parts of a string. It's a backwards way of searching so I doubt it's possible.

I think part of it would have something to do with:

REPLACE( event_name, ' ', '') 

and

REPLACE( venue_name, ' ', '') 

to get rid of the space.

I also thought REGEX might come into it.

Absolutely no idea where to start! PSUEDOCODE might be:

CONCAT(
event_name = REPLACE( part of :search, ' ', '')
,
venue_name = REPLACE( part of :search, ' ', '')
)
= :search

If I used noddybobstheatre as the search term I want to search the column event_name for part of it and venue_name for another part and when the 2 parts are put together they equal the search term.

event_name = 'noddy'
venue_name = 'bobs theatre'

Like I said, this might be a crazy ask...

用这个,

concat(replace(event_name,' ',''),replace(venue_name,' ',''), )=:search

Reading what you need I thought that the like statement should use the column and not the search value.

here is my reproduction of what I understood from your problem

CREATE TABLE movies
    (`word1` varchar(5), `word2` varchar(7))
;

INSERT INTO movies
    (`word1`, `word2`)
VALUES
    ('micko', 'to'),
    ('he', 'llbrane'),
    ('mick', 'oto'),
    ('hell', 'brane')
;

then here is the sql statement that I used

select * from movies where 'mickoto' like CONCAT(Replace(word1,' ',''),'%') and 'mickoto' like CONCAT('%',Replace(word2,' ',''))  

all you have is to adapt this to your context

here is the fiddle execution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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