Hyphen within string in mysql query causing strange behavior

database, mysql, ruby-on-rails, sql

Solution

You need to include quotations.

SELECT * FROM `point_allocations` WHERE (user_id = '1234-567abc89')

Because the `user_id` column expects character-typed data, it will take your value (1234-567abc89) and parse it as an integer, truncating the content after the hyphen. If you include it in quotations, it will accept it as a string and transfer properly.

Enjoy and good luck!

Problem

In my Rails testing environment, I have a `user_id` that looks like `1234-567abc89`. I'm getting inconsistent behaviour by querying this user in different tables. Most of the queries are working, but running one particular query fails: ``` ActiveRecord::StatementInvalid (Mysql::Error: Unknown column '1234' in 'where clause': SELECT * FROM `point_allocations` WHERE (user_id = 1234-567abc89) ): ``` So for some reason, everything beyond the hyphen is getting cut off. I realized that for the queries that work, it is looking up user `1234` instead of `1234-567abc89`, but if all the others work, any idea why only this one would return an error?

Original source