Take the 2-minute tour ×
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. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 down vote accepted

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

SELECT * FROM
(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
add comment

Your Answer

 
discard

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.