Starbody
Starbody

Reputation: 153

Mysql where clause accepting integer with strings on primary key field

In MySql i have a primary key field id, when i do select * from users where id='12p' it brings the value of row 12.

Please how do I go about this.

Updated Nodejs sample

const { User } = require('models')

User.findOne({where:{id:'12p'}}).then(user => console.log(user))

Upvotes: 1

Views: 529

Answers (1)

Suraj Singh
Suraj Singh

Reputation: 89

In MYSQL, While you put your string values in where clause it parses integer number out of it. For instance, If you put '1abc', it'll parse 1 out of it. Follow the other examples:

'abc' => 0,
'2abc' => 2,
'a2bc' => 0,
'1a1bc' => 1

I don't know exactly Why does this happen but that's how MySQL works. I myself was struggling with that bug in my job. I had to add some extra logics to get the valid response.

Upvotes: 2

Related Questions