Search code examples
mysqlsqlselectsql-like

how to do a select on like values from multiple columns in sql


I have two tables with columns:

Genres  
  ID  
  genre

and

Adjectives
  ID
  adjective_title

I need to do a select that returns the matching values from both tables columns with the like syntax.

For example if ep was the value entered using like the results would look like:

result_column:
--------------
epiphonic -- (from genres table)
epic      -- (from adjectives table)

etc . . .

I'm pretty sure I need to use a subquery to return the results.


Solution

  • try this

    SELECT genre AS result
    FROM genres
    WHERE genre LIKE '%ep%'
    UNION
    SELECT adjective_title AS result
    FROM 
      adjectives
    WHERE adjective_title LIKE '%ep%'
    

    The union will eliminate duplicates in each query use UNION ALL for each.