Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have SELECT field(foo,'this','that','bar','baz') FROM t WHERE f1 IN ('this','that','bar','baz') How can I avoid listing ('this','that','bar','baz') twice?

share|improve this question
up vote 2 down vote accepted

Try using the FIELD function's return value and make sure it is not zero:

(SELECT *,field(foo,'this','that','bar','baz') fieldfoo FROM t) A
WHERE fieldfoo > 0;
share|improve this answer
OK that avoids the strings but still requires writing out N "things" for N strings. – chx Apr 16 '12 at 6:40
Updated my answer – RolandoMySQLDBA Apr 16 '12 at 15:56
Ah yes! FIELD is 1-indexed and returns 0 for anything not found, indeed. Nice trick. – chx Apr 16 '12 at 19:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.