mm1975
mm1975

Reputation: 1655

Search in other column if value not exists

I search for a term in the column synonyms in my table. In the same table, there is also a column id. Now I would like to search the term in synonyms at first. If there is no value in this column, I would like to search in the column id for the term.

SELECT * FROM `someTable`
WHERE synonyms
LIKE ?
LIMIT 50

Upvotes: 0

Views: 28

Answers (1)

slaakso
slaakso

Reputation: 9080

You can use ifnull.

where ifnull(synonyms, id) LIKE ?

And if your If there is no value means also an empty string, you can use case

where 
  case 
     when synonyms is null or synonyms='' then id 
     else synonyms 
  end LIKE '%ab%'

Upvotes: 1

Related Questions