Reputation: 323
This is a pretty basic problem, but I'm new to SQL so it's giving me trouble.
Say I have three tables: X, Y, and Z.
X contains foreign keys for Y, and Y contains foreign keys for Z. How can I select all rows in X which have foreign keys for Y, which in turn have foreign keys for Z, which in turn match some specified value?
For example lets say that these are the columns for each table:
X = "x_ids", "x_values", "y_foreign_keys"
Y = "y_ids", "y_values", "z_foreign_keys"
Z = "z_ids", "z_values"
I need to find out how to select all rows of X which I can trace down to z_values which equal the string "test".
Upvotes: 0
Views: 57
Reputation: 156
Can you try this:
Tables
X = "x_ids", "x_values", "y_foreign_keys"
Y = "y_ids", "y_values", "z_foreign_keys"
Z = "z_ids", "z_values"
SQL
SELECT * FROM Z
INNER JOIN Y ON z_ids = z_foreign_keys
INNER JOIN X ON y_ids = y_foreign_keys WHERE X.x_values= 'HELLO WORLD';
Upvotes: 1