Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query where a field value does not contain empty spaces

Tags:

sql

mysql

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.

like image 638
Lars Avatar asked Feb 16 '11 22:02

Lars


People also ask

How do you exclude a space in SQL?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do I select a column that contains spaces in SQL?

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).

Does SQL ignore trailing spaces?

Takeaway: According to SQL Server, an identifier with trailing spaces is considered equivalent to the same identifier with those spaces removed.


1 Answers

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?

like image 134
Phil Avatar answered Sep 25 '22 18:09

Phil