2

My current MySql wild card is LIKE '%A%B%'. This can return values that contain A and B.

Can anyone suggest how can I alter the wildcard statement to return values that contain either A or B.

Thanks in advance.

Andre
  • 26,751
  • 7
  • 36
  • 80
User420
  • 137
  • 3
  • 12
  • 3
    you can do something like `LIKE '%A%' or columnname LIKE '%B%'` . It might not be accurate. but give it a try – Nad Aug 18 '16 at 05:27
  • Thanks for the answer. But is there any other way to get the result by just altering the wildcards. @stack – User420 Aug 18 '16 at 05:33
  • 1
    No, there is not, at least not with the `like` operator. With `rlike` operator it is possible to do it. – Shadow Aug 18 '16 at 05:37
  • @User420: I m not sure about any other way. but the syntax which I gaved you works perfectly(_tested_). – Nad Aug 18 '16 at 05:38

2 Answers2

2

You can use REGEXP

 select * from Table1 where some_column REGEXP '[AB]'

there are lots of different ways in writing this as a regular expression, the above basically means containing A or B.

Generally you want to avoid using REGEXP and LIKE '%something' because the do not use indexes. Thus for large tables these operations would be unusable. When you want to do a search of this kind it's always best to stop and ask: "Have I got the best database design?", "Can I use full text search instead?"

e4c5
  • 52,766
  • 11
  • 101
  • 134
2

You can add as many like operator you want within the parenthesis with OR condition like below

select * from tablename where (column_name  like '%test%' or same_column_name  like '%test1%' or 
            same_column_name  like '%test2%' or same_column_name  like '%test3%')

For more info have a look at the below link.

SQL Server using wildcard within IN

Hope that helps you

Community
  • 1
  • 1
Nad
  • 4,605
  • 11
  • 71
  • 160
  • Indeed. This does work. However there is a major performance drawback. Both REGEXP and LIKE '%something%' are notoriously slow. So combining them makes things worse. – e4c5 Aug 18 '16 at 05:44
  • I totally agree with you on this. But other than this, I didn't find any way to help OP. May be some senior people here and figure out – Nad Aug 18 '16 at 05:46
  • no worries. I didn't think this was a bad answer. +1 from me – e4c5 Aug 18 '16 at 05:46
  • Thanks and appreciated. :) – Nad Aug 18 '16 at 05:47