sagar
sagar

Reputation: 745

Why LIKE is giving error in mysql

I have database in which a table operator_details has two columns...

mobile_prefix    operator

9899              idea
9214              airtel

like this, now I am trying a query in which if the number is 9899745214 ., it should return IDEA. I am trying it in this way...

SELECT operator 
FROM operator_details
WHERE mobile_prefix + '%' LIKE '9899745214' 

i HAVE ALSO TRIED LIKE THIS :-

SELECT operator 
FROM operator_details
WHERE '9899745214' LIKE  mobile_prefix + '%' 

But both giving errors... where have i done mistake???

Upvotes: 1

Views: 149

Answers (4)

Ullas
Ullas

Reputation: 11566

Use SUBSTRING

QUERY

SELECT operator FROM tbl
WHERE mob_pre = SUBSTRING('9899745214',1,4)

FIND FIDDLE HERE

Upvotes: 0

Rajesh Mbm
Rajesh Mbm

Reputation: 844

In the first case you are tying to perform string operations in mysql query which is not allowed.You should use functions like concat('','') to achieve this. Db column name must be followed by where clause.but in your 2nd case, you are directly giving value for where clause which is not allowed

Upvotes: 0

Michael Durrant
Michael Durrant

Reputation: 96614

Try using:

WHERE mobile_prefix LIKE '%'+'9899745214'+'%' 

Upvotes: 0

Linger
Linger

Reputation: 15068

Why not something like:

SELECT operator 
FROM operator_details
WHERE mobile_prefix = LEFT('9899745214', 4)

Upvotes: 2

Related Questions