sql查询包含所选列的某些单词

时间:2021-07-28 01:42:10

Please help I wanna write a script that contains certain word. e.g:

请帮助我想写一个包含某些单词的脚本。例如:

 table name : sample
 +-------+-------+
 |column1|column2|
 +-------+-------+
 |1      |help   |
 +-------+-------+

table name : sample2
+-------+-------+
|column1|column2|
+-------+-------+
|1      |help me|
+-------+-------+

I try write a script like this:

我尝试写一个这样的脚本:

SELECT *FROM SAMPLE S
JOIN SAMPLE2 S2 ON S.COLUMN1 = S2.COLUMN2
WHERE S.COLUMN2 LIKE S2.COLUMN2

but the "where clause" is does not work any one know how to write the correct script? if any please answer my question

但是“where子句”是不行的,任何人都知道如何编写正确的脚本?如果有的话请回答我的问题

2 个解决方案

#1


1  

This where clause should ideally be part of the JOIN.

理想情况下,where子句应该是JOIN的一部分。

You need to prefix and suffix the wildcard % using string concatenation in order for the like to work:

您需要使用字符串连接为通配符%添加前缀和后缀,以使其能够工作:

SELECT *FROM
SAMPLE S JOIN SAMPLE2 S2
ON S.COLUMN1 = S2.COLUMN1
And S2.COLUMN2 LIKE '%' || S.COLUMN2 || '%';

#2


0  

Change the join condition so they are both using COLUMN1 and then you want the to match:

更改连接条件,以便它们都使用COLUMN1,然后您希望匹配:

'help me' LIKE '%help%'

So:

所以:

SELECT *
FROM   SAMPLE S
       JOIN SAMPLE2 S2
       ON (   S.COLUMN1 = S2.COLUMN1
          AND S2.COLUMN2 LIKE '%' || S.COLUMN2 || '%' )

#1


1  

This where clause should ideally be part of the JOIN.

理想情况下,where子句应该是JOIN的一部分。

You need to prefix and suffix the wildcard % using string concatenation in order for the like to work:

您需要使用字符串连接为通配符%添加前缀和后缀,以使其能够工作:

SELECT *FROM
SAMPLE S JOIN SAMPLE2 S2
ON S.COLUMN1 = S2.COLUMN1
And S2.COLUMN2 LIKE '%' || S.COLUMN2 || '%';

#2


0  

Change the join condition so they are both using COLUMN1 and then you want the to match:

更改连接条件,以便它们都使用COLUMN1,然后您希望匹配:

'help me' LIKE '%help%'

So:

所以:

SELECT *
FROM   SAMPLE S
       JOIN SAMPLE2 S2
       ON (   S.COLUMN1 = S2.COLUMN1
          AND S2.COLUMN2 LIKE '%' || S.COLUMN2 || '%' )