wyatt
wyatt

Reputation: 3246

MySQL query enclosing numbers in strings

Suppose I have a query

SELECT * FROM table WHERE x > y;

where "y" is a user-input value. I'd like to enclose y in quotes. So far as I've tested, the query works properly when the quotes are added. Is the behaviour defined? Is it known to result in an efficiency drop?

Note that the query is an example fabricated for simplicity. Also, this is not an attempt to deal with SQL injection.

Upvotes: 1

Views: 164

Answers (3)

Mithun Sasidharan
Mithun Sasidharan

Reputation: 20920

If the field that you are trying to compare with is a numeric field ie int, then all the possible combinations must work in mysql

select * from users where id = 20;

select * from users where id = "20";

select * from users where id = '20';

Refer the MySQL DOC for more depth

Upvotes: 3

outis
outis

Reputation: 77400

The conversion behavior of MySQL when comparing different types is well-defined. When a string and a number are compared, both are converted to floats. It's outlined in the manual, § 11.2.

Upvotes: 0

ajreal
ajreal

Reputation: 47321

IMO, you can try :-

cast( "-10" as signed )

This is casting in mysql (the function name is cast, obvious?).

Upvotes: -1

Related Questions