jondinham
jondinham

Reputation: 8511

How to apply MySQL 'LIKE' to a column which is specified in the query?

I need to combine 2 columns and find whether the combined content matches something. However the following query doesn't work:

select 
  concat(column1,column2) as combined_column 
from 
  my_table 
where 
  combined_column like '%value%';

MySQL reports an error that 'combined_column' doesn't exist. How to solve this problem?

Upvotes: 0

Views: 104

Answers (2)

sasfrog
sasfrog

Reputation: 2460

select 
  concat(column1,column2) as combined_column 
from 
  my_table 
where 
  Concat(column1,column2) like '%value%';

Upvotes: 3

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22162

You should replace your where clausole with this one:

where concat(column1,column2) like '%value%';

Explanation: The where clausole isn't able to read the var named in the SELECT.

Upvotes: 3

Related Questions