alex
alex

Reputation: 4914

What the mysql query difference between =1 and = '1' with enum?

I had a query like

SELECT name, town FROM clients WHERE course = 1;

and it gave me some results.

But i noticed the query didn't output the latest entries. I look into the table clients with phpmyadmin and i saw more course fields with value 1. After i changed the query to

SELECT name, town FROM clients WHERE course = '1';

i got the right output.

My question: why is this? and why did i get some results with the first query ?

ps: course is an enum field ( '0','1').

Upvotes: 1

Views: 98

Answers (1)

Marty McVry
Marty McVry

Reputation: 2856

1 is either an integer value, or 'TRUE', or ... (ambiguous) while '1' is a string value.

As explained in the MySQL-documentation for the ENUM-type:

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.

[...]

If you wish to use a number as an enumeration value, you must enclose it in quotation marks. If the quotation marks are omitted, the number is regarded as an index. For this and other reasons—as explained later in this section—we strongly recommend that you do not use numbers as enumeration values.

So, ENUM is a string object, therefore you need to reference to it as a string.

Upvotes: 3

Related Questions