我有一个包含两个表Team
和User
的数据库。
每个团队都可以拥有一个或两个用户。
我希望选择表Team
的输出,以便包含表User
中的两个用户的信息。
一旦我定义了表结构,它就会更容易理解。
表Team
:
+-----------+-------------------+-------------------+-----------------+
| team_name | user_one | user_two | team_note |
+-----------+-------------------+-------------------+-----------------+
| Team one | skuhsa@jdds.dfd | kgihse@kdhf.dfj | one to twenty |
| Team two | dsjgknsz@djfd.fkg | | three to thirty |
+-----------+-------------------+-------------------+-----------------+
表User
:
+-------------------+-----------+-----------------+
| email | user_name | user_note |
+-------------------+-----------+-----------------+
| skuhsa@jdds.dfd | skuhsaone | gimme money |
| kgihse@kdhf.dfj | kgihse | drop it |
| dsjgknsz@djfd.fkg | dsjgknsz | just eat it |
+-------------------+-----------+-----------------+
我正在寻找的输出是这样的。
+-----------+--------------------+-----------+------------------+-----------+-----------------+
| team_name | user_one | user_name | user_two | user_name | team_note |
+-----------+--------------------+-----------+------------------+-----------+-----------------+
| Team one | skuhsa@jdds.dfd | skuhsaone | kgihse@kdhf.dfj | kgihse | one to twenty |
+-----------+--------------------+-----------+------------------+-----------+-----------------+
| Team two | dsjgknsz@djfd.fkg | dsjgknsz | | | three to thirty |
+-----------+--------------------+-----------+------------------+-----------+-----------------+
我很好地感觉它可以很容易地完成,但是现在我正在尝试各种各样的JOIN和东西,并最终得到重复的结果或行。 如果有任何PostgreSQL特定方法可以做到这一点,那对我来说就好了。
SELECT * FROM Team LEFT JOIN User ON Team.user_one=User.email
有效,但如何只选择我想要的列?即,如何区分第一个和第二个用户的b / w列?
答案 0 :(得分:1)
答案实际上很简单,因为评论只是使用LEFT join
,这可以说LEFT JOIN [TableName]
SELECT * FROM [Team]
LEFT JOIN [User]
另外看看
http://www.w3schools.com/sql/sql_join_left.asp
编辑:
你的第二个选择是这样做:
SELECT * FROM [Team]
UNION
SELECT * FROM [User]
答案 1 :(得分:1)
一些left join
应该可以解决问题:
SELECT team_name, user_one, u1.user_name, user_two, u2.user_name, team_note
FROM team t
LEFT JOIN user u1 ON t.user_one = u1.email
LEFT JOIN user u2 ON t.user_one = u2.email
答案 2 :(得分:0)
可以使用inner join或left join从两个表中提取数据。
但查询结果将更类似于以下内容:
#links a {
color: red;
text-decoration: none
}
#links a:visited {
color: goldenrod;
text-decoration: none
}
#links a:hover {
color: maroon;
text-decoration: none;
font-variant: small-caps
}
这是最好的解决方案,因为您不知道团队中有多少用户如果他们是3(或100?)会发生什么?但它与您的期望不同,因此请检查您是否同意该表格。
答案 3 :(得分:0)
除此之外,你应该看foreign key
。外键将表链接在一起。我认为你的桌子必须像。
+-----------+-------------------+-------------------+-
| team_name | team_note | team_id(Primary key)
+-----------+-------------------+-------------------+
| Team one | one to twenty | 1
| Team two | stuff | 2
+-----------+-------------------+-------------------+
+-------------------+-----------+-----------------+-------------------------------------------------
| email | user_name | user_note | f_team_id(foreign_key) to team.team_id
+-------------------+-----------+-----------------+--------------------------------------
| skuhsa@jdds.dfd | skuhsaone | gimme money | 1
| kgihse@kdhf.dfj | kgihse | drop it | 1
| dsjgknsz@djfd.fkg | dsjgknsz | just eat it | 2
+-------------------+-----------+-----------------+------------------------------------------
之后
SELECT * FROM team JOIN user ON user.f_team_id=team.team_id; OR
SELECT * FROM team, user WHERE user.f_team_id=team.team_id;