How to Use Wildcards in "In" MySQL Statement

Can I use wildcards in IN MySQL statement?

I'm not sure it's any better than what you came up with but you could use MySQL's regex capabilities:

select * from my_table where field rlike 'apple|orange';

Also, as others have mentioned, you could use MySQL's full text search capabilities (but only if you're using the MyISAM engine).

mysql cant add wildcard in IN Operator

Your query doesn't work because your '%' is being interpreted as the modulo operator which is a binary operator and as such makes your syntax invalid.

 SELECT * FROM `cdr` WHERE `src` like '5%' OR src LIKE '9725%' OR src LIKE '05%';

If you want to use the '%' character as a wild card it has to be quoted and used with the LIKE operator.

Types of Wildcards in MySql

Thanks everyone!

For specific this question, we need to use regexp

Select * From tableName Where ColumnName Regexp "^[PST]";

For more detail over Regular Expression i.e Regexp :

https://www.youtube.com/watch?v=KoltE-JUY0c

Using wildcards with the MYSQL IN Clause

Not really. You are better off with regular expressions. Something like this:

where customerid regexp 'apple|pear|banana'

This will return customer ids that have 'apple' or 'pear' or 'banana' anywhere.

If you really wanted to use like you need multiple comparisons:

where customerid like '%apple%' or
customerid like '%pear%' or
customerid like '%banana%'

MySQL Wildcards * and %

* can only be used as a wildcard (or truncation) in a full text search while % (match 0 or more characters) and _ (match exactly one character) are only applicable in LIKE-queries.

SQL In() with Wildcard Operators For MULTIPLE COLUMNS

You could just concatinate them with a separator.

SELECT * 
FROM your_table
WHERE CONCAT_WS('|', col1, col2, col3, col4) LIKE '%text%'

In MySql 8 one could also use an EXISTS for this

SELECT * 
FROM your_table t
WHERE EXISTS (
select 1
from (
select t.col1 as col union all
select t.col2 union all
select t.col3 union all
select t.col4
) q
where col LIKE '%text%'
);

Demo on db<>fiddle here

How to use LIKE and wildcards in mysql statements

You could use MySQL function FIND_IN_SET:

SELECT * FROM tbl
WHERE
FIND_IN_SET('16', REPLACE(kategorien, ';', ','))>0

however, it is usually not a good idea to store comma separated values in a single field, please have a look at this question: Is storing a delimited list in a database column really that bad?

SQL - How to use wildcard in LIKE as a normal character

try this :

select * from TABLE where VALUES like  '%[%]%'


Related Topics



Leave a reply



Submit