tags:

views:

54

answers:

2

I've been trying to select the status of doing a LIKE comparison:

SELECT (`my_column` LIKE '%thepattern'%) AS `did_match`

Thanks!

Edit: Just to clarify this was used so I could get this:

SELECT
     (CASE WHEN `title` LIKE '%keyword%' THEN 20 ELSE 0 END) +
     (CASE WHEN `desc` LIKE '%keyword%' THEN 10 ELSE 0 END)
     AS `match`
FROM myTable
ORDER BY `match`
+2  A: 

You need to use:

SELECT t.my_column AS did_match
  FROM YOUR_TABLE t
 WHERE t.my_column LIKE '%thepattern%'

Replace YOUR_TABLE with the name of your table...

Without a FROM clause, MySQL (and SQL Server) will allow the query, but not against any data.

OMG Ponies
This will work if he only wants columns that do match. You are right on the from <table>.
Imre L
+1  A: 

Not sure I understand the question, and it looks like OMG Ponies has figured it out better than I have, but in case this helps:

SELECT CASE WHEN `my_column` LIKE '%thepattern%' 
       THEN 'did_match' ELSE 'no_match' END CASE AS MYTEST
  FROM ...

Link to CASE statement documentation.

Adam Bernier
Adam: I'm not 100% that I understand what the OP intended. Hopefully they'll clarify
OMG Ponies
Thanks guys, two potential solutions is better than one!
Kristopher Ives
Just wanted to add that this helped me a lot!
Kristopher Ives