1

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))
7
  • 1
    if id column is a int datatype the result MySQL returns is "correct" because '12p' is auto casted to a int by MySQL.. Thats why you should NOT ever use single qoutes on a int datatype. Note other database systems will give a error when using single qoutes on a int datatype. Commented Jul 26, 2018 at 17:46
  • I am using a ORM in nodejs (sequelize) and give this issue. Commented Jul 26, 2018 at 17:53
  • The 12p is in a variable, so do I remove the single quotes? Commented Jul 26, 2018 at 17:54
  • Show the node.js code? Commented Jul 26, 2018 at 17:57
  • I have updated the question. Commented Jul 26, 2018 at 20:04

1 Answer 1

2

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.

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

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.