Using LIKE in a JOIN query

喜你入骨 提交于 2019-12-06 10:01:35

It probably is, though this might depend on the Database you are using. For example, in Microsoft SQL, it would work to use somthing like this:

SELECT *
FROM [Table1] INNER JOIN [Table2]
ON LEFT([Table1].[Customer Name],4) = LEFT([Table2].[Customer],4)

Syntax may be different if using other RDBMS. What are you trying this on?

Seems like this should work:

Select *
From Table1, Table2 
Where Table1.CustomerName Like Cat('%',Trim(Table2.CustomerName),'%')

Yes, that's possible. But I doubt, that every name in table 2 only has 4 letters, so here's a solution where the name in table2 is the beginning of the name in table1.

Concat the string with a %. It's a placeholder/wildcard for "anything or nothing".

SELECT
*
FROM
Table1
INNER JOIN Table2 ON Table1.CustomerName LIKE CONCAT(Table2.Customer, '%');

Concatenating of strings may work differently between DBMS.

If you are only trying to match first four Characters you can use following :

SELECT --your columns
FROM Table1 T1
JOIN Table T2
ON
SUBSTRING ( T1.CustomerName ,1, 4) = SUBSTRING ( T2.Customer ,1, 4)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!