简体   繁体   English

MySQL加入条件与减法

[英]MySQL Join Condition with Subtraction

I am joining two tables on a TINYINT column, which works perfect, like: 我在TINYINT列上加入了两个表,它们非常完美,如:

table1 INNER JOIN table2 ON table1.col = table2.col

Now I would like to join records, where value of table1.col is greater by one than table2.col: 现在我想加入记录,其中table1.col的值比table2.col大1:

table1 INNER JOIN table2 ON table1.col = (table2.col - 1)

Unexpectedly that returns an empty result. 出乎意料的是,返回一个空结果。

Why? 为什么? Can someone clarify that and help me? 有人可以澄清并帮助我吗?

Thanks in advance, Sascha. 在此先感谢,Sascha。

Your join condition is subtracting the wrong column - since table1.col is greater by one, to equalize the values you need to subtract 1 from it, or add one to table2.col , for example: 您的加盟条件减去错列-因为table1.col是更大的一个,以平衡需要从它减去1,或添加一个到值table2.col ,例如:

table1 INNER JOIN table2 ON (table1.col - 1) = table2.col

For a concrete example, suppose a row in table 1 where table1.col=8 needs to be matched with a row in table2 where table2.col=7 - you need to subtract 1 from 8 or add 1 to 7. 举一个具体的例子,假设table 1中的一行,其中table2.col=7 table1.col=8需要与table2中的一行匹配,其中table2.col=7 - 你需要从8中减去1或者添加1到7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM