MySQL 使用 like 来匹配特定的字符串

MySQL using like to match specific string

在我的专栏中,我可以使用类似这样的字符串:"data+" 或 "data+data+data+..(undefined times)..+" 我只需要 获取我有多个数据的列,而不仅仅是一个。

我试过

mycol NOT LIKE '%+'

但是没用...

实际上我不知道数据是一个变化的字符串:'blabla' 或 'aString' 或 'whatever'

如果我的专栏里有

'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'

我只想select

'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+', 

不是'dvgff+'

%是通配符。您应该在 data 之后使用 %。试一试:

SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'

上面的查询会过滤掉所有在data+之后有任何字符的记录。

如果你想搜索 '..xxx+..' 那么你应该使用 xxx+%

如果你想搜索 '..+xxx..' 那么你应该使用 %+xxx

如果你想搜索 '..++..' 那么你应该使用 %++%

如果你想搜索 '..+..+..' 那么你应该使用 %+%+%


It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...

所以你可以尝试 like '%+%+%' 只排除一个 '+'

CREATE TABLE TestTable
    (`text` varchar(90))
;

INSERT INTO TestTable
    (`text`)
VALUES
    ('jdsjpgsg+jdsjpgsg+'),
    ('dvgff+'),
    ('ffef+eefds+ghghgh+')
;

select * from TestTable
where text like '%+%+%'

|               text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |

SQL Fiddle Demo Link