7

I am currently using the follow query:

SELECT *
FROM `wp_usermeta`
WHERE meta_key='avatar'
AND meta_key NOT LIKE '% '
ORDER BY RAND()
LIMIT 4

In that way, I want to try to get only field values, where no empty spaces re in the file name. Where is the error in my query? It still selects filenames with empty spaces in the filename.

1 Answer 1

15

Try

NOT LIKE '% %'

Your current wildcard match only catches trailing spaces.

Also, you're using meta_key twice. Should the column used in your LIKE clause be meta_value (or whatever it is in Wordpress).

This question is probably worth reading if you're concerned about performance - Which is faster — INSTR or LIKE?

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

3 Comments

yeah..probably the second meta_key was wrong...ist should be meta value. I tested it before with % %. Thanks for your help!
okay. I read it and decided for this way: 'SELECT * FROM wp_usermeta WHERE meta_key='avatar' AND NOT INSTR(meta_value,'% %') ORDER BY RAND() LIMIT 4' works fine!
@Lars You don't need wildcards in INSTR. Also, I'd steer clear of automatic boolean conversion. Be specific, use AND INSTR(meta_value, ' ') = 0. Don't know why you're changing from the NOT LIKE query as the performance is the same as INSTR and the query would be easier to understand

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.