views:

32

answers:

3

I have a table called records which has several columns, one of which is fromphone which represents a phone number. I can run this query to see the different phone numbers:

SELECT DISTINCT fromphone
FROM records

and it shows the different phone numbers.

However, if I run this query:

SELECT *
FROM records
WHERE fromphone = '123-456-7890'

where fromphone is a phone number in the table, no results get returned.

A: 

Are you sure there is a record with phone number as 123-456-7890 in the database table? There maybe some leading or trailing space in it.

Try to use LIKE and %% operator, do it like this:

SELECT *
FROM records
WHERE fromphone LIKE '%123-456-7890%'
shamittomar
That worked. Not sure why/how leading or trailing spaces are in there
JPC
+1  A: 

The most likely scenario is that the exact string, '123-456-7890' is not in that column. Try using LIKE instead of =, in case there's some extra spaces or something thats causing the equal not to match the phone number.

GrandmasterB
A: 

If you have leading/trailing spaces, you can just use TRIM()

SELECT *
FROM records
WHERE TRIM(fromphone) = '123-456-7890'

Using LIKE with the leading and trailing wildcard can get very slow if you have a lot of rows in the records table because MySQL has to do a full table scan when you do that.

Stephen Wuebker