Mysql Wildcard in field

416 Views Asked by At

I have a mysql database and my table is :

Name      | passcode
----------------------
hi*       | 1111
----------------------
j?n       | 2222

i want this query has a result like this :

Select * from Users Where Name='hiiiiiiiiiiii'

The Query result is my first row and * and ? works like a wildcard but i use it in my database field

2

There are 2 best solutions below

4
On

You dont want to put the wildcard in the actual value stored in the database field. To solve this example you would need to concat the wild card in your query...

SELECT * FROM `Users` WHERE 'hiiiiii' LIKE CONCAT('%',`Name`,'%') 
0
On

You can tyr regexp:

SELECT * FROM Users WHERE 'hiiiiii'                REGEXP Name; -- hi*
SELECT * FROM Users WHERE 'jn'                     REGEXP Name; -- j?n
SELECT * FROM Users WHERE 'salaaaaaaaaaaaaaaaaaam' REGEXP Name; -- Sala*m

but you should be careful, because:

SELECT * FROM Users WHERE 'say hiiii to all!' REGEXP Name; -- hi*
SELECT * FROM Users WHERE 'Jn'                REGEXP Name; -- j?n
SELECT * FROM Users WHERE 'n'                 REGEXP Name; -- j?n

that's why you should specify more strict pattern at field name...
And i suppose that it will be not best solution in performance...