1

I am trying to make select query as follow

SELECT * FROM `corporate_names` WHERE id = '1s';

but it still gives me result as there is no column id with value equals to '1s'. it reslts me of SELECT * FROM corporate_names WHERE id = '1'; result.

I am not getting why isthis happening? how can i exactly match id column with string value as id is primary key with int column type.

i want it to exact match the column value

7
  • '1s' will never match an integer column. Your question is very unclear. With your rep at 188, you should know how to ask a question. Check out the links in my next comment for some helpful information., Commented Aug 16, 2018 at 11:52
  • 1
    You are expected to try to write the code yourself. After doing more research if you have a problem post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented Aug 16, 2018 at 11:52
  • i just want to match '1s' with column id, and it is giving me result where id=1, it should not give result as we know id column can not have value '1s', i want it to exact match Commented Aug 16, 2018 at 11:57
  • 1
    If you tell MySQL to compare an integer and a string and don't tell MySQL what to do, it has three options: a) convert the integer to a string and compare as a string b) make an attempt to convert the string to an integer (with several sub-options like remove all non-digit things, treat a string as a flat 0, ...) and compare as an integer c) throw an error. You know now what MySQL chose. If you do not like that choice: tell MySQL what you want. In this case, cast the id to a string. Alternatives are e.g. to check if your input is a valid integer and not executing that query at all. Commented Aug 16, 2018 at 12:08
  • @Solarflare thank you. your comment was helpful. i did it as SELECT * FROM corporate_names where CAST(id as char(255)) = '1s'. thank you. Commented Aug 16, 2018 at 12:25

3 Answers 3

4

When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa. example

SELECT 1+'1'; -> 2

The following rules describe how conversion occurs for comparison operations:

For details you can read documentation

But you can stop it following way

SELECT case when  CONVERT('1s' USING utf8) = cast( 1 as char) then 2 else 0 end; this will return 0 .

so explicit conversion is the way of stopping this or solving your problem

Sign up to request clarification or add additional context in comments.

1 Comment

@VishalB i checked a lot of documentation of mysql and found only explicit conversion is the way of solving your peoblem
3

I solve this as below

SELECT * FROM `corporate_names` where CAST(id as char(255)) = '1s'.

now it is exactly matching value with column value, i just cast id column to char.

Comments

0

if you would like to get all the corporate names where the id starts with 1 then use LIKE as shown below:

SELECT * FROM `corporate_names` WHERE id LIKE '1%';

Else if you would like to retrieve the results for just a specific id use the following:

SELECT * FROM `corporate_names` WHERE id='1';

I hope that helps.

4 Comments

i want it to exact match, as if column has value '1s' give result otherwise not, but it is giving me result as 'id' column does not has value '1s'.
@VishalB It should give you nothing if it does not countain 1s. It should say 0 row(s) returned. You should be using the query SELECT * FROM corporate_names WHERE id='1s'; to accomplish ths
thats the question says.. in my query id = 1s but it still giving me result
SELECT * FROM corporate_names WHERE id LIKE '1s';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.