简体   繁体   中英

MySQL Join Condition with Subtraction

I am joining two tables on a TINYINT column, which works perfect, like:

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 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.

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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