Reputation: 745
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
Reputation: 11566
Use SUBSTRING
SELECT operator FROM tbl
WHERE mob_pre = SUBSTRING('9899745214',1,4)
Upvotes: 0
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
Reputation: 15068
Why not something like:
SELECT operator
FROM operator_details
WHERE mobile_prefix = LEFT('9899745214', 4)
Upvotes: 2