简体   繁体   English

在2列中搜索MYSQL中的字符串部分

[英]Search 2 columns for parts of string in MYSQL

I would like to search inside 2 columns for parts of a string. 我想在2列内搜索字符串的一部分。 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. 我还认为REGEX可能会加入其中。

Absolutely no idea where to start! 绝对不知道从哪里开始! PSUEDOCODE might be: PSUEDOCODE可能是:

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. 如果我将noddybobstheatre用作搜索词,我想在event_name列中搜索其中的一部分,而在场地名中搜索另一部分,将这两个部分放在一起时,它们等于搜索词。

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. 阅读您需要的内容后,我认为like语句应使用该列而不是搜索值。

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 然后这是我使用的sql语句

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 这是小提琴执行

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

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