[英]How to JOIN two tables when not using any Primary Keys?
I'm trying to JOIN
2 tables using columns that aren't either of the tables respective Primary Keys: 我正在尝试使用两个表都不是主键的列来JOIN
2个表:
SELECT *
FROM TableA A
INNER JOIN TableB B
ON A.col5 = B.col5
Yet the above is returning 0 results
even though I know for sure that there are rows in Table A
whose col5
values match values in col5
of Table B
. 然而,上面是返回0 results
,即使我知道肯定有行Table A
,其col5
值匹配值col5
的Table B
。
What am I doing wrong? 我究竟做错了什么?
You query: 您查询:
SELECT *
FROM TableA A
INNER JOIN TableB B
ON A.col5 = B.col5;
Has the correct syntax for a join. 具有正确的联接语法。 If there are matching values, then it will return it. 如果有匹配的值,则它将返回它。 (Or course, you could be calling this from an application and there could be errors either in the application code or the connection to the database, but that is another issue.) (或者,当然,您可以从应用程序中调用此函数,并且应用程序代码或与数据库的连接中可能存在错误,但这是另一个问题。)
Some cases where values look the same but are not: 在某些情况下,值看起来相同但不相同:
abc
is the same as ABC1
, but SQL doesn't. 您认为abc
与ABC1
相同,但是SQL却不同。 And two more reasons that I can think of but may not be true in all databases: 我可以想到但又并非在所有数据库中都适用的另外两个原因:
Your query is correct and should return the matching values in the table. 您的查询是正确的,应返回表中的匹配值。 But as you said there is invisible space in the values in other table at the end try using LTRIM and RTRIM to remove the invisible space from the values. 但是正如您所说的,最后在其他表中的值中存在不可见空间,请尝试使用LTRIM和RTRIM从值中删除不可见空间。 LTRIM removes the whitespaces from the beginning of the string and RTRIM removes the whitespaces from the end of the string. LTRIM从字符串的开头删除空格,而RTRIM从字符串的结尾删除空格。
The query would be like : 查询如下:
SELECT * FROM TableA A INNER JOIN TableB B ON A.col5 = LTRIM(RTRIM(B.col5)) SELECT * FROM TableA A ANER JOIN TableB B ON A.col5 = LTRIM(RTRIM(B.col5))
I hope it helps you out. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.