Access/SQL Server 2008 Join using Like not working

安稳与你 提交于 2021-02-19 08:04:56

问题


I apologize ahead of time because this question is in various forms many times on this site, but none of the solutions I have tried implementing seem to work properly, and I cannot find an answered question that gets me the right answer.

I am working in access for a co-worker, though it will probably be in SQL in the future once he can get a server set up. I am trying to join two tables using Like, which is easy enough, except that it just is not working properly. First, an example from each table:

T1

+-------------------------+--------------------------------------------------------------+--------------+
|LOCATION_CG_LOC_LEGACY_ID|LOCATION_CODE_DESC                                            |PI_LOW_SIDE_MW|
+-------------------------+--------------------------------------------------------------+--------------+
|55555-Opt01              |Autobot Transformer, 800/900 NETWORK, Optimus #1 - 800 NETWORK|5             |
+-------------------------+--------------------------------------------------------------+--------------+
|55555-Opt02              |Autobot Transformer, 800/900 NETWORK, Optimus #2 - 800 NETWORK|6             |
+-------------------------+--------------------------------------------------------------+--------------+
|55555-Opt03              |Autobot Transformer, 800/900 NETWORK, Optimus #3 - 800 NETWORK|6.5           |
+-------------------------+--------------------------------------------------------------+--------------+

T2

+------+-----------------------------------------+------------+
|DIVID |DMT Bank Name                            |5 Digit Code|
+------+-----------------------------------------+------------+
|647531|800/900 NETWORK, Optimus #1 - 800 NETWORK|55555       |
+------+-----------------------------------------+------------+
|647532|800/900 NETWORK, Optimus #2 - 800 NETWORK|55555       |
+------+-----------------------------------------+------------+
|647533|800/900 NETWORK, Optimus #3 - 800 NETWORK|55555       |
+------+-----------------------------------------+------------+

I am trying to combine like items essentially, so that I can perform whatever queries I want. For now however, I am fine with a select all. I just need to combine like items. In T1, the first column [LOCATION_CG_LOC_LEGACY_ID] is a non-repeating unique item. In T2, [DIVID] is a non-repeating unique item. I was trying to query them together using

SELECT *
FROM [Data Table 1] INNER JOIN [Data Table 2] ON (([t1].[LOCATION_CODE_DESC] Like [t2].[DMT Bank Name]
OR [t2].[DMT Bank Name] Like [t1].[LOCATION_CODE_DESC]) AND ([T1].[LOCATION_CG_LOC_LEGACY_ID] Like [t2].[5 Digit Code] or [t2].[5 Digit Code] Like [T1].[LOCATION_CG_LOC_LEGACY_ID]));

Now, I think there is a problem with the second half of that join condition, but even using only the first half, I get ZERO results. However, if I edit the middle column in each table, then use the first half of that condition, it works perfectly. In other words, if I make the columns match exactly, the query seems to work, which defies the whole point in using a "like" join to begin with.

I have not yet tried this in SQL Server, but I need to get it working in Access anyway as a proof of concept at least. Any help you wonderful ladies and gentlemen can give me would be appreciated, even if it is pointing me to another thread that I have missed like a dolt.

Lastly, I am trying to avoid using VBA if possible.


回答1:


You need to add wildcards such as % when using LIKE in sqlserver, otherwise it essentially becomes a check for equality (=). here is your query with wildcards:

SELECT *
FROM  t1 
INNER JOIN  t2 
ON [t1].[LOCATION_CODE_DESC]   Like '%' + [t2].[DMT Bank Name]+ '%' 
AND [T1].[LOCATION_CG_LOC_LEGACY_ID] Like '%' + CONVERT(varchar(50),[t2].[5 Digit Code] )+ '%';

Here is a fiddle: http://sqlfiddle.com/#!3/dc2b9/10/0

You'll also need to convert to int to a varchar of some sort for compatibility.

Note though, that doing a join on a like, especially with a convert will result in very poor performance for large data sets.

Edit: MS Access

MS Access will be different when you convert int to string, and uses different wildcards. I don't have access installed so can't test, but I think your MS Access query would look like this:

SELECT *
FROM  t1 
INNER JOIN  t2 
ON [t1].[LOCATION_CODE_DESC]   Like '*' & [t2].[DMT Bank Name] & '*' 
AND [T1].[LOCATION_CG_LOC_LEGACY_ID] Like '*' & CStr( [t2].[5 Digit Code] ) & '*';



回答2:


Try this and look closely in like format

--MS SQL
SELECT  *
FROM    T1
        Inner Join T2 on T1.[LOCATION_CG_LOC_LEGACY_ID] Like T2.[5 Digit Code] + '%'

--MS ACCESS 
SELECT  *
FROM    T1
        Inner Join T2 on T1.[LOCATION_CG_LOC_LEGACY_ID] Like T2.[5 Digit Code] & '%'    


来源:https://stackoverflow.com/questions/21152835/access-sql-server-2008-join-using-like-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!