Using LIKE in MySQL SELECT fields
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`
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.
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.
精彩评论