1

This is kind of a weird quirk I noticed and I really just wanted to see if someone could provide insight as to why its happening.

Lets say we have a field called "account" that's a varchar(x)

These two queries will return the same result set.

SELECT * FROM table WHERE account = 1234;
SELECT * FROM table WHERE account LIKE '1234%';

I stumbled on this by accident in a script I wrote to migrate some older data and I accidentally forgot to encapsulate my string.

Can anyone tell me why this behavior happens?


update:

I feel like I may have no explained this well enough so I'm going to provide a sample table:

table_name: id - INT account - varChar(50)

entries:

ID - account
1 - "1"
2 - "12"
3 - "123"
4 - "1234"

so:

1:

SELECT * FROM table_name WHERE account LIKE '1%'

will return entries 1, 2, 3, 4

2:

SELECT * FROM table_name WHERE account = 1

will return entries 1, 2, 3, 4

3:

SELECT * FROM table_name WHERE account = '1'

will return entry 1

Why is 1 the same as 2, and why is 2 not the same as 3

3 Answers 3

3

http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html

"...MySQL automatically converts numbers to strings as necessary, and vice versa."

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

4 Comments

I understand that mysql does automatic conversions, what I'm more interested in is the LIKE statement that seems to become implied.
Could we clarify this a little? 1) What behavior did you expect? 2) How was it different from what you observed?
I see, after your edit. Looks like you are right. It's indeed implies LIKE, similarly to Perl & Co. Simplifying, the answer is, "Because it's MySQL." ;) It used to be strange but evolved into a decent product. Seriously, I think that behavior is wrong and would submit a bug report.
Honestly that's what I expected to hear I just wanted to make sure I wasn't interpreting something wrong or just misunderstood how MYSQL interprets these types of conversions. Thanks for the input.
3

This is because % is a meta character which means any number of characters including zero characters. Like keyword looks for the pattern which begins with 1234. Since account = 1234 which also matches the criteria, you get the same result.

Comments

1

I think at this point this has been discovered as a MYSQL bug, I filed a bug report id #67427 if anyone is interested in getting an update I would direct you to view it through http://bugs.mysql.com/

Comments

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.