I am attempting to perform an inner join on two tables so that I can round a value by the precision associated with that value’s pid in the other table. However, it is returning NULL for pid 3. I believe it has to do with the NULL precision for pid 2, because when I set it’s precision to a non-NULL value, I get the correct value for pid 3 in the query below.
How can I restructure this query so that it will always return the correct value even if another pid contains a NULL precision?
select pid_values.pid, pid_values.value, round(value, pids.precision)
from pid_values inner join pids on pid_values.pid = pids.pid;
+------+--------+------------------------------+
| pid | value | round(value, pids.precision) |
+------+--------+------------------------------+
| 1 | 10.123 | 10 |
| 2 | 5.5 | 6 |
| 3 | 3.1234 | NULL |
+------+--------+------------------------------+
Data in both tables:
select * from pids;
+------+-----------+
| pid | precision |
+------+-----------+
| 1 | 0 |
| 2 | NULL |
| 3 | 1 |
+------+-----------+
select * from pid_values
+----+------+--------+
| id | pid | value |
+----+------+--------+
| 1 | 1 | 10.123 |
| 2 | 2 | 5.5 |
| 3 | 3 | 3.1234 |
+----+------+--------+
Try this query: