2

I have used ENUM in a table of a MySQL database. How can I get all stuff where type = a, b, c?

Simply, if I use VARCHAR, then I can use:

SELECT * FROM stuff WHERE type IN ("a","b","c")

But using ENUM it does not work.

How can I get all stuff where type = a, b, c?

Note: Type is used as ENUM not CHAR or VARCHAR.

2 Answers 2

2

You can use FIELD like so:

SELECT * FROM `stuff` WHERE FIELD(`type`, "a", "b", "c");

That should be the proper syntax to apply the logic from your question. It all boils down to weird type casting issues with the way ENUM lists work - Ideally the best way to go is setup forign key relation ship with a table that houses something like:

TypesTbl
+---------------+
| AK  |  Value  |
+---------------+
|  1  |    a    |
|  2  |    b    |
|  3  |    c    |
+---------------+
Stuff
+------------------+
| PK  | * |  type  |
+------------------+
|  1  |   |   1    |
|  2  |   |   2    |
|  3  |   |   1    |
|  4  |   |   3    |
+------------------+

SELECT s.* FROM `Stuff` AS s LEFT JOIN `TypesTbl` t ON s.type = t.AK WHERE t.Value IN ('a', 'b', 'c');

That way you use a left join (or whichever fashion) in your query and you don't have to update the DDL just to add a new ENUMerated value. The first part of the answer I believe supports what you're trying to do. The second part was just additional information.

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

2 Comments

@ marco sorry but it is really not worked for reference check out stackoverflow.com/questions/3146173/…
You may wish to just edit this question rather than post a whole new one.
0

Use single quotes:

SELECT * FROM stuff WHERE type IN ('a', 'b', 'c');

3 Comments

it is not my problem but when i run it all is found in result. i found all even if his type = d , e, f
Post the DDL for the stuff table then - this definitely works and filters appropriately for me...
I agree - what is the actual DDL for the table you're using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.