1

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
  • 1
    typo? should be '%thepattern%' Commented Jun 9, 2010 at 12:42
  • Yeah, %keyword% and %thepattern% are the same in my example. Came back and posted the edit later on, using different working sample code. Commented Jun 10, 2010 at 2:38

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This will work if he only wants columns that do match. You are right on the from <table>.
1

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.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.