I have some data in a database which I want to display on a website. I want to query the storename_history table to get all data where the title contains what the user searches (in this instance the word Pink.)
My database table looks like this:
productId | title | price | isAvailable |
---|---|---|---|
ABC123DEF | Pink | 0.45 | 1 |
My query:
SELECT * FROM storename_history WHERE title = "%Pink%"
This query works with every column except for title. How's this possible?
It does not give an error: it just finds nothing.
CodePudding user response:
Your query may not work because the "Title" field may have "hidden" spaces in its values.
If you want to ensure that you always get your matching strings, you should use the LIKE
operator.
SELECT *
FROM storename_history
WHERE title LIKE "%Pink%"
If you want to investigate more on spaces, you can check the length of your string by using the CHAR_LENGTH
function.