像SQL Inner Join中的运算符一样

时间:2017-01-24 13:59:42

标签: mysql join

我有三张桌子:

表1:

Handle       Name Description
OP1,BP1,33:A OP1  OP1 Desc

表2:

Handle       Attribute Value
OP1,BP1,33:A Z_MARK    X

表3:

Handle CurrentHandle ValuationPoint
Z:Y:Z  OP1,BP1,33:#  Disc Roll 

现在我正在尝试加入这三个表,所以我提出的查询是:

Select t1.Handle , t1.Name , t1.Description from table1 t1
inner join table2 t2 on t2.Handle = t1.Handle
inner join table3 t3 on t2.Handle like CONCAT('%', t3.CurrentHandle, '%')

但像往常一样,我没有记录。

在三个表中,table1和table2对于Handle列具有相同的值,其中table2的Handle与table3的CurrentHandle Column几乎相似。

所以我试图用Like子句加入这三个表。

3 个答案:

答案 0 :(得分:1)

我认为你也比较#太...试着替换它:

Select 
      t1.Handle , 
      t1.Name , 
      t1.Description 
from 
      table1 t1 inner join table2 t2 
      on t2.Handle = t1.Handle
      inner join table3 t3 
      on t2.Handle like CONCAT('%',REPLACE( t3.CurrentHandle,'#',''), '%')

希望这有帮助!

答案 1 :(得分:1)

如你所愿:

Select t1.Handle , t1.Name , t1.Description from table1 t1
inner join table2 t2 on t2.Handle = t1.Handle
inner join table3 t3 on t2.Handle like CONCAT('%', t3.CurrentHandle, '%')

不返回OP1,BP1,33:A!= %OP1,BP1,33:#%

这假设#末尾的t3CurrentHandle是您描述变量或不需要的字符,然后使用:

SELECT t1.Handle , t1.Name , t1.Description 
FROm table1 t1
INNER JOIN table2 t2 ON t2.Handle = t1.Handle
INNER JOIN table3 t3 ON t2.Handle LIKE CONCAT('%', SUBSTRING(t3.CurrentHandle, 1, CHAR_LENGTH(t3.CurrentHandle)-1)., '%')

答案 2 :(得分:0)

您可以使用SOUNDEX进行模糊搜索/比较(https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex

Select t1.Handle , t1.Name , t1.Description from table1 t1
inner join table2 t2 on t2.Handle = t1.Handle
inner join table3 t3 on SOUNDEX(t2.Handle) = SOUNDX(t3.CurrentHandle)