简体   繁体   English

不使用任何主键时如何联接两个表?

[英]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值匹配值col5Table 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: 在某些情况下,值看起来相同但不相同:

  • Both values are floats. 这两个值都是浮点数。 They look the same when printed out. 打印时它们看起来相同。 But the bit at the end of the value differs. 但是值末尾的位不同。 NEVER USE FLOATs FOR EQUI-JOINs. 切勿将浮标用于同等联接。
  • One value is a number and the other value is a string. 一个值是一个数字,另一个值是一个字符串。 The conversion of one of the values results in a slightly different value. 值之一的转换导致稍有不同的值。
  • One value is a date/time and the other is a string. 一个值是日期/时间,另一个是字符串。 The conversion of one of the values results in a slightly different value. 值之一的转换导致稍有不同的值。
  • The values are strings. 值是字符串。 They differ in case. 它们大小写不同。 You think abc is the same as ABC1 , but SQL doesn't. 您认为abcABC1相同,但是SQL却不同。
  • You have spaces or other "hidden" characters at the end of the string. 字符串末尾有空格或其他“隐藏”字符。

And two more reasons that I can think of but may not be true in all databases: 我可以想到但又并非在所有数据库中都适用的另外两个原因:

  • One value is a char and the other varchar (or wide versions of them). 一个值是char,另一个值是varchar(或它们的宽版本)。 The implicit spaces at the end of the char make them different. 字符末尾的隐式空格使它们有所不同。
  • One is a unicode string and the other ASCII string. 一个是unicode字符串,另一个是ASCII字符串。 Two characters look the same but are not. 两个字符看起来相同,但不同。

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.

相关问题 当有两个外键引用同一个主键时,如何在 SQL 中的两个表上进行连接? 这让我发疯 - How do I make a join on two tables in SQL when there are two foreign keys referencing the same primary key? It's driving me insane 如何将具有两个不同主键的两个表联接到另一个表中? - How can I join two tables with two different primary keys into another table? 内部联接两个表,都具有没有主键的外键? - Inner join two tables, both have foreign keys with no primary key? 如何使用相同的主键联接两个表? - How to join the two tables with the same primary key? 当外键不同时,如何在MySQL中将主键连接到两个外键? - How to join a primary key to two foreign keys in MySQL when the foreign keys will be different? 如何在使用内部联接合并两个表时将列设置为PRIMARY KEY(两个联接表中没有主键) - How to set a column to PRIMARY KEY while merging two tables using inner join?(there is no primary key in the two joining tables) SQL 两个表两个主键如何连接? - SQL Two tables two primary keys how to connect them? 如何使用两个外键作为一个主键将域表与第二个表连接起来? - How to join domain table with second table using two foreign keys for one primary key? 如何比较两个表的主键,这是五列的组合? - How to compare primary keys two tables, which is combination of five columns? 为什么向用于连接两个表的两个键之一添加索引会使执行时间比根本不使用任何索引要慢? - Why does adding an index to one of the two keys used to join two tables make the execution time slower than not using any index at all?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM