I've been trying to write a simple keyword comparison query in mysql but for some reason an all numeric keyword won't match an all numeric search term.
I have a table of products, a table of keywords and joining table. To make things simpler I'm using a view which performs the join, giving me a simple list of keyword strings per product id. 1 product in particular has 4 keywords, "John", "Deere", "8000", "Midroller" and these show up correctly in the view and when joined.
Where I am having trouble is that (keyword = "8000") is never true for the "8000" keyword.
Example
SELECT a.id, kw.keyword AS keyword, (kw.keyword = 'John' OR
kw.keyword = 'Deere' OR
kw.keyword = "8000" OR
kw.keyword = 'Midroller') AS matched
FROM `kc5n2_product` AS a
INNER JOIN `product_keywords` AS kw ON a.id = kw.product
WHERE a.id =119
returns
id keyword matches 119 8000 0 119 John 1 119 Deere 1 119 Midroller 1
I've had a look at this: MySql: Compare 2 strings which are numbers? but it didn't really help.
I also found this MySQL Query doesn't seem to be outputting expectations but I'm trying trying to compare them as strings, not as numbers.
Even trying
SELECT a.id, kw.keyword AS keyword, (CAST(kw.keyword as Char(4)) = CAST("8000" as Char(4))) AS matched
to try to force mysql to interpret them both as strings didn't help at all.
I found that it will match the search term if I use
kw.keyword LIKE "%8000%"
but I'd prefer to avoid LIKE if I can as at this stage I would like to keep the search fairly restrictive.
Is there something obvious I am doing wrong?