Anton Sementsov
Anton Sementsov

Reputation: 1246

Search phone number from MYSQL DB

I have a table with phone column. Phones in it looks like this +1 (123) 456-3780. If user input in search field "35" i should show him all users with phones:

+3 (534) 000-1010

+1 (350) 000-9090

+1 (003) 534-2343

+1 (001) 223-5323 etc.

i have tried to do something like that

SELECT * FROM `mytable` WHERE `phone` LIKE '%3%5'

but this is not the best solution, beacause query get other phones like that +3 (000) 000-500.

I would like to use RLIKE or something like that, but do not know how create needed regular expression and how should it look?

Upvotes: 2

Views: 2473

Answers (2)

Matt Bidas
Matt Bidas

Reputation: 1

REGEXP '^(.{2})+(\.{4}[-].{4})$' this pattern is for phone numbers with ()

example: (00)0000-0000

Upvotes: -1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

You need a [() -]* between each input character:

SELECT * FROM `mytable`
WHERE `phone` REGEXP '3[() -]*5'

This allows punctuation to exist inside your digit "string" but nothing else.

Upvotes: 4

Related Questions