Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a help to write search query

Tags:

sql

php

mysql

My database contains a list of phone numbers which is of varchar type. Phone number may be in any of these formats

12323232323
1-232-323 2323
232-323-2323
2323232323

Instead of the symbol there may be ( ) , . or space And if I search for 12323232323, 1-232-323 2323, 232-323-2323, or 2323232323 it should display all these results. I need to write a query for this.

like image 773
Juice Avatar asked Nov 09 '11 04:11

Juice


2 Answers

I think it is not efficient to do this realtime, I propose two options.

  1. clean the data, so there will be only one format.

  2. add another column which contains the clean data, so when you search, you search for this column, when display you can display the various format data.

like image 160
James.Xu Avatar answered Sep 28 '22 17:09

James.Xu


I agree with James, but if you really need to search the database as it is, perhaps MySQL's REPLACE operator will get you where you need to go. Something like

select * from mytable where replace(crazynumber,'-','')='23232323';
like image 40
fimbaz Avatar answered Sep 28 '22 19:09

fimbaz