Suppose I have a table called my_table with primary_key table_pk and with three fuzzy fields (foreign keys) with the names X_fk, Y_fk and Z_fk, respectively, and another field called data. Tables X, Y, and Z have a primary key field and a name field (for example, {X_pk, X_name}, {Y_pk, Y_name}, {Z_pk, Z_name}).
I need a query that uniquely returns a row from my_table, given the names in tables X, Y and Z.
SELECT table_pk
FROM my_table
WHERE
X_fk = (SELECT X_pk FROM X WHERE X_name = ?)
AND
Y_fk = (SELECT Y_pk FROM Y WHERE Y_name = ?)
AND
Z_fk = (SELECT Z_pk FROM Z WHERE Z_name = ?)
This does not work when I want to find the string (X_Name, Y_Name, Z_Name) = ('XXX', 'YYY', NULL), because Z_fk = NULL can never result in TRUE. How can I modify the above query to get a unique record from a table that takes into account unique records, where some foreign keys are NULL?
source
share