# select * from users ;
user_id | login | email | password | firstname | lastname
---------+--------+-------------------------+----------+-----------+----------
1 | katie | katie.s@ymail.com | secret | Katie | Secret
(1 row)
# select * from forum;
forum_id | forum_name | group_id | forum_parent
----------+------------+----------+--------------
1| 1st forum | 1 | -1
(1 row)
# select * from groups;
group_id | group_name | group_description | group_founder
----------+-----------------------+-------------------------------------------------+---------------
1 | Java programming | System.out.println("Hello world in Java!"); :) | 1
(1 row)
我的数据库中有这3个表。我想从用户id = 1
创建的论坛中获取。这该怎么做? (在表groups
中,创建此类组的用户的id
称为group_founder
)。
我的意思是,通过上面的输出,您可以看到具有id = 1
的用户创建了一个组“Java编程”,然后在该组中创建了一个名为“1st forum”的论坛。请帮助:)
答案 0 :(得分:3)
试试这个
SELECT *
FROM forum f
JOIN groups g ON g.group_id = f.group_id
JOIN users u ON u.user_id = g.group_founder
WHERE u.user_id = 1
答案 1 :(得分:3)
根据您的输入,查询可能就是这样
SELECT U.login,U.email,U.firstname
FROM users U JOIN groups G ON U.user_id = G.group_founder
JOIN forum F ON G.group_id = F.group_id
WHERE U.user_id = 1