I am creating a “simple” search query. I’d like to select a column that should have the value 1 if a specified column LIKE(‘test’), it’s kind of hard to explain.
What i want to do is like this
SELECT *,(CASE mycol LIKE('%test%') as match THEN 1 END) FROM mytable
So if mycol matches the condition, then match=1, if else 0. How would i do that? Sorry if hard to understand.
You are nearly there, but you have made four errors and used one bad practice:
ELSE 0in your CASE expression.CASEexpressions need one or moreWHENexpressions.matchas an alias.Also, don’t use
SELECT *.Try this:
However there’s no need for the CASE expression in MySQL because TRUE and 1 are equivalent: