[英]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. 我需要做一个选择,以
like
语法从两个表列中返回匹配值。
For example if ep was the value entered using like
the results would look like: 例如,如果ep是使用
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. 联合将消除每个查询中的重复项,对每个查询都使用UNION ALL。
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: 您可以在没有子选择的情况下执行此操作,但是通过使用concat来构建使用like运算符的字符串,在大型表上将不会很快:
select g.ID, g.genre, a.adjective_title from Genres g, Adjectives a where
concat(g.genre, a.adjective_title) like '%epic%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.