在mysql查询中字符串中的连字符导致奇怪的行为

时间:2022-06-29 10:17:31

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:

在我的Rails测试环境中,我有一个user_id,看起来像1234-567abc89。通过在不同的表中查询这个用户,我得到了不一致的行为。大多数查询都是有效的,但是运行一个特定的查询失败:

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?

所以出于某种原因,连字符之外的所有东西都被切断了。我意识到,对于那些有效的查询,它查找的是用户1234,而不是1234-567abc89,但是如果其他所有的都有效,知道为什么只有这个会返回错误吗?

1 个解决方案

#1


5  

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.

因为user_id列需要字符类型的数据,所以它将获取您的值(1234-567abc89)并将其解析为一个整数,在连字符后截断内容。如果你把它包含在报价中,它会接受它作为一个字符串,并适当地转移。

Enjoy and good luck!

享受,祝你好运!

#1


5  

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.

因为user_id列需要字符类型的数据,所以它将获取您的值(1234-567abc89)并将其解析为一个整数,在连字符后截断内容。如果你把它包含在报价中,它会接受它作为一个字符串,并适当地转移。

Enjoy and good luck!

享受,祝你好运!