开发者

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
  adjec开发者_高级运维tive_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.


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.


You can do this without sub-selects, but it won't be quick on large tables, by using concat to build the string you use the like operator on:

select g.ID, g.genre, a.adjective_title from Genres g, Adjectives a where
concat(g.genre, a.adjective_title) like '%epic%'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜