1

I'm trying to return a value of true or false (or yes/no, etc.) as part of a select if a particular field in a table contains a substring.

  1. Select the ID, name, and whether or not a person is subscribed.
  2. A field called emailList has a comma separated list of names which should be used to check if the requester is subscribed. The result of the substring search should yield the true/false value as the result in another field.

The basic query would look like this:

SELECT
    id,<...>,name
FROM
    table

In the <...> area, I would want something equivalent to:

`emailList` contains @input ? "Yes" : "No"

I can't figure out how to do this to save my life. I'm guessing it can be done in other ways, but this seems like a good learning opportunity. Any suggestions?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Anthony
  • 1,760
  • 1
  • 23
  • 43

1 Answers1

10

Use IF():

SELECT
    id,
    IF(emailList LIKE '%string%', 'Yes', 'No') AS OnEmailList,
    name
FROM
    table

Just replace the word "string" with the search term you're looking for or a variable in your server-side programming language of choice.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Duh! Thanks. I swear I looked for everything except the most obvious programming construct ever created. – Anthony Apr 26 '13 at 03:04