开发者

mysql query not giving correct results

开发者 https://www.devze.com 2023-01-14 23:28 出处:网络
I have a table called records which has several columns, one of which is fromphone which represents a phone n开发者_Go百科umber.I can run this query to see the different phone numbers:

I have a table called records which has several columns, one of which is fromphone which represents a phone n开发者_Go百科umber. 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.


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.


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%'


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号