MySQL - WHERE <column> LIKE '$string'
Hey all
I have a frustrating problem, but it should be simple enough for experienced MySQL users.I have a Column of type TEXT which holds items separated by commas. I want to Select all rows based on a comparison of a string fragment, and here is how I'm doing it.
I have a table of books, called BOOK_TITLES
CATEGORY is a column in my table, and most often has only 1 item, such as "item1_", but it could have "item1_,item2_,item3_"
now $category carries the current category in question, which would look like "item1_" for example.
I want to grab all the CATEGORY rows that contain the current $category. I was trying to use:
--
SELECT * FROM BOOK_TITLES WHERE CATEGORY LIKE '%$category%'"
--
The problem here is that ANY row whose CATEGORY column contains $category will be selected. even though the string "item1_" does not equal "item12_", because it is similar enough, it gets picked.
so, i just dropped the %'s
--
SELECT * FROM BOOK_TITLES WHERE CATEGORY LIKE '$category'"
--
Now, even though I am using LIKE, it is still treating it like '='.
Meaning: if $category = "item1_", and CATEGORY = 'item1_', then it gets added, but if CATEGORY = 'item1_,item2_' it doesn't get added.
What is the SELECT syntax to search for an exact substring of a COLUMN value?
thanks for the wonderful suggestions that I know are coming my way.
aaaaannnd the answer is....!!
using '_' is a problem because MySQL see's that as a comparison operator. if you insist on using _ in a MySQL field, you must escape the character in the search criteria: 'item1_' becomes 'item1\_'
I have a frustrating problem, but it should be simple enough for experienced MySQL users.I have a Column of type TEXT which holds items separated by commas. I want to Select all rows based on a comparison of a string fragment, and here is how I'm doing it.
I have a table of books, called BOOK_TITLES
CATEGORY is a column in my table, and most often has only 1 item, such as "item1_", but it could have "item1_,item2_,item3_"
now $category carries the current category in question, which would look like "item1_" for example.
I want to grab all the CATEGORY rows that contain the current $category. I was trying to use:
--
SELECT * FROM BOOK_TITLES WHERE CATEGORY LIKE '%$category%'"
--
The problem here is that ANY row whose CATEGORY column contains $category will be selected. even though the string "item1_" does not equal "item12_", because it is similar enough, it gets picked.
so, i just dropped the %'s
--
SELECT * FROM BOOK_TITLES WHERE CATEGORY LIKE '$category'"
--
Now, even though I am using LIKE, it is still treating it like '='.
Meaning: if $category = "item1_", and CATEGORY = 'item1_', then it gets added, but if CATEGORY = 'item1_,item2_' it doesn't get added.
What is the SELECT syntax to search for an exact substring of a COLUMN value?
thanks for the wonderful suggestions that I know are coming my way.
aaaaannnd the answer is....!!
using '_' is a problem because MySQL see's that as a comparison operator. if you insist on using _ in a MySQL field, you must escape the character in the search criteria: 'item1_' becomes 'item1\_'
