您连接的是数据库,而不是表,通过连接数据库,您可以访问其中的表。
因此,一旦您连接到数据库,要获取列表,您需要使用/运行类似于以下内容的查询:
SELECT Pictures.name, Users.name
FROM Pictures
JOIN Albums ON Pictures.ALBUM_ID = Albums.ID
JOIN Users ON Albums.ALBUM_ID = Users.USER_ID
;
例子
表格
作为一个例子,连接的数据库有以下表格(注意表格名称和列名已经为方便而更改):
![enter image description here](https://istack.dev59.com/ZOw49.webp)
结果
使用类似上述(只是不同的表格和列名)的查询:
SELECT _pictures.name, _users.name
FROM _pictures
JOIN _albums ON _pictures.albumid = _albums.id
JOIN _users ON _albums.userid = _users.id
;
结果为:
![在此输入图片描述](https://istack.dev59.com/bgYKA.webp)
该示例基于以下SQL来创建和填充表:
CREATE TABLE IF NOT EXISTS _users (ID INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE IF NOT EXISTS _albums (ID INTEGER PRIMARY KEY, name TEXT, userid INTEGER);
CREATE TABLE IF NOT EXISTS _pictures (ID INTEGER PRIMARY KEY, name TEXT, albumid INTEGER);
INSERT INTO _users VALUES
(1,'Fred'),
(2,'Bert'),
(3,'Harry')
;
INSERT INTO _albums VALUES(1,'Ablum owned by Fred',1),
(2,'Another Album owned by Fred',1),
(3,'Another Album owned by Bert',2),
(4,'Another Album owned by Harry',3),
(5,'An Album for Fred',1),
(6,'The Album of Harry',3),
(7,'Harry the Album',3),
(8,'A taste of Bert',2)
;
INSERT INTO _pictures VALUES
(1,'Picture for Album 1',1),
(2,'Picture for Album 2',2),
(3,'Picture for Album 3',3),
(4,'Picture for Album 1',4),
(5,'Picture for Album 1',5),
(6,'Picture for Album 1',6),
(7,'Picture for Album 1',7),
(8,'Picture for Album 1',8)
;