First, if you want the column playlistOwner
of the table Playlist
as foreign key that references the column id
of the table User
you should define with the same data type as the referenced column.
So the correct definition should be playlistOwner INTEGER
:
CREATE TABLE IF NOT EXISTS Playlist (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
image BLOB,
private NUMERIC,
playlistOwner INTEGER,
FOREIGN KEY(playlistOwner) REFERENCES User(id)
)
and the values that you store in the column playlistOwner
must be the id
s of the users and not their username
s.
Now if you know the user's id
there is no need for a join.
You simply do:
SELECT *
FROM Playlist
WHERE playlistOwner = 10;
If you know only the username
then you join the tables:
SELECT p.*
FROM Playlist p INNER JOIN User u
ON u.id = p.playlistOwner
WHERE u.username = 'somename'
or with a subquery in the WHERE
clause:
SELECT *
FROM Playlist
WHERE playlistOwner = (SELECT id FROM User WHERE username = 'somename')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…