I have three tables in my database; walls, keys and wallAccessKeys.
CREATE TABLE walls (
id SERIAL PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO walls (name)
VALUES ('Test Wall');
CREATE TABLE keys (
id SERIAL PRIMARY KEY,
key VARCHAR(50)
);
INSERT INTO keys (key)
VALUES ('testingtesting123');
CREATE TABLE wallAccessKeys (
id SERIAL PRIMARY KEY,
keyID INT,
wallID INT
);
INSERT INTO wallAccessKeys (keyID, wallID)
VALUES (1, 1);
Walls should only be accessible with certain keys, the keys that access each wall is stored in the wallAccessKeys table.
Walls have many keys, keys open many walls.
I'm trying to write an SQL statement to select wall.ID given keys.key value.
I've gotten so far using an Inner Join on wall and wallAccessKeys but can't figure this out.
SELECT *
FROM walls
INNER JOIN wallAccessKeys
ON walls.ID = wallAccessKeys.wallID
Is using Inner join the best way to do this? If so please, could someone help me with the SQL?
To summarize, I need an SQL statement where I can provide a key value e.g. testingtesting123, and get a list of walls that key has access to.
Managed to figure this out using another inner join. Solution bellow if anyone needs it.
SELECT walls.id
FROM walls
INNER JOIN wallAccessKeys
ON walls.id = wallAccessKeys.wallID
INNER JOIN keys
ON keys.id = wallAccessKeys.keyID and keys.key = 'testingtesting123'