简体   繁体   中英

Need to join double precision foreign key with integer primary key

What is the highest performing query that joins a double precision foreign key to an integer primary key. The tables are from a data vendor, and I'd rather not convert the column data type.

In this example, fk is a double precision and pk is an integer.

Should I use round?

SELECT this_table.pk, other_table.some_col FROM this_table
INNER JOIN other_table ON other_table.pk = ROUND(this_table.fk) 

Or maybe no conversion is needed?

Thanks.

You can join integers and doubles directly. MySQL will manage the type casting itself. ROUND(double) returns a double anyway, not an integer.

The joins will succeed only for the doubles that represent an integer. That means you can directly join 6.0 to 6; you can't directly join 6.000001 to 6.

If you want to join 6.000001 to 6, cast the double to integer to join values in the range 6.0 <= 6 < 7.0 . . .

select ...
from table_with_integers n
inner join table_with_doubles d 
  on cast(d.double_column as unsigned integer) = n.integer_column
...

or use ROUND() to join values in the range 6.0 <= 6 < 6.5.

The type cast makes more sense if the doubles are supposed to represent integers. But if the doubles are supposed to represent integers, you shouldn't be getting values like 6.000001 in the first place.

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